PHP DateTime Format: Current, Tomorrow, Yesterday, Week & Month

Create Date Format in PHP DateTime CodeSnippet

Here’s a featured snippet from the article that shows how to create date format in PHP.

//Initializes an object representing the current date and time in the default timezone.
$now = new DateTime();
 
//Object Oriented Approach
echo $now->format('Y-m-d'); //2022-08-24
 
//Procedural Approach
echo date_format($now, 'Y-m-d');  //2022-08-24

Read more to learn how to get current, tomorrow, yesterday, next week, and month day in PHP. So fasten your seat belts for the time travel.

What is DateTime Class in PHP?

PHP DateTime class contains many attributes and methods for representing dates and times in PHP. This article uses the DateTime class and its methods in all the examples. A cool thing to know is that all the methods have a function alias. So, if you’re not used to the object-oriented approach, just switch to the procedural.

How to Create Date Format in PHP
“A Calendar”

First things first, we need to create an object of the DateTime class. The following examples invoke the constructor function in more than a couple of different ways to initialize a few PHP new DateTime objects.

 
//Initializes an object representing the current date and time in the default timezone.
$now = new DateTime();
 
//Initializes an object with the specified date in the default timezone.
$ninety = new DateTime('1999-02-11');
 
//Initializes an object with the current date and time in US/New York.
$newyork_now = new DateTime('now',new DateTimeZone('America/New_York'));

Let’s just stick to the $now object and use that in the following examples.

Ultimate PHP DateTime Date Format Guide

Let’s jump straight away to the examples and learn how to create date format in PHP.

Get the Current date in YYYY-MM-DD Format in PHP using DateTime Object

The DateTime object features a method format that expects a string. The string specified the output format. The official documentation tabulates all the format characters and descriptions, but the following example uses the string for the YYYY-MM-DD format.

//Initializes an object representing the current date and time in the default timezone.
$now = new DateTime();
 
//Object Oriented Approach
echo $now->format('Y-m-d'); //2022-08-24
 
//Procedural Approach
echo date_format($now, 'Y-m-d');  //2022-08-24

The example uses two ways to get the current date in YYYY-MM-DD in PHP – Object Oriented & Procedural Approach.

The date_format function is an alias of the format method. Both the approaches work fine. Pick the one that suits you best.

The important parameter is the format string (Y-m-d), where:

Y  – A full numeric representation of a year. For instance, 2022.

m – A numeric representation of a month with leading zeros. For instance, 08.

d  – A representation of the day with leading zeros. For instance, 02.

Great! That was simple. Besides, the methods in the example above can also return information like month or day. In the following example, PHP gets a month from the date object.

//Gives us object representing the current date and time in the default timezone.
$now = new DateTime();
 
//Object Oriented Approach
echo $now->format('m'); //08
 
//Procedural Approach
echo date_format($now, 'M');  //Aug

Observe that changing the string format parameter changes the output. 

That’s for now, but what’s tomorrow’s date in PHP? Let’s learn how to get tomorrow’s date in PHP.

Create Date Format in PHP for Tomorrow with DateTime Object

One way to get tomorrow’s date in PHP would be to initialize a new DateTime object as follows.

$tomorrow = new DateTime(‘tomorrow’);
 
echo $tomorrow->format('Y-M-d'); //2022-Aug-25

The example initializes a new DateTime object similar to what we have done before. However, another way is to add to the current date and get back the date for tomorrow.


PHP DateTime features modify method that changes the object based on a string modifier.

$date = new DateTime("now");
 
$date->modify('+1 day');
 
echo $date->format('Y-M-d'); //2022-Aug-25

Cool! The modify method adds the offset based on the modifier string ‘+1 day’. Quite intuitive. 
Wait! There’s another method that adds to the DateTime object, and it goes by the name add.

 
$date = new DateTime("now");
 
$date->add(new DateInterval("P1D")); //P1D means an interval of one day.
 
echo $date->format('Y-M-d'); //2022-Aug-25

The add method expects a DateInterval object that specifies the offset. The ‘P1D’ stands for an interval of one day.

Next, let’s learn how to go back in time – How to get Yesterday Date in PHP?

Create Date Format in PHP DateTime for Yesterday

The examples will be pretty much similar as we have seen above, except that PHP gets yesterday’s date here.

Here’s how to create the date format in PHP for yesterday.

$yesterday = new DateTime('yesterday');
 
echo $yesterday->format('Y-M-d'); //2022-Aug-23

Following this example, let’s use modify method to get yesterday’s date in PHP.

$date = new DateTime("now");
 
$date->modify('-1 day');
 
echo $date->format('Y-M-d'); //2022-Aug-23

PHP DateTime comes with a sub method that subtracts from the date time. Let’s use that in the following example to get yesterday’s date in PHP.

$date = new DateTime("now");
 
$date->sub(new DateInterval("P1D")); //P1D means an interval of one day.
 
echo $date->format('Y-M-d'); //2022-Aug-23

Voila! Up next, we’ll see how to get next week’s date in PHP.

Get Next Week’s Date in PHP DateTime

Now, as we are quite familiar with the methods of the DateTime object, you’ll guess the two methods to get the next week’s date in PHP – modify & add.

Let’s see the example using modify first.

$date = new DateTime("now");
 
$date->modify('+1 week');
 
echo $date->format('Y-M-d'); //2022-Aug-31

Next, let’s see the add method.

$date = new DateTime("now");
 
$date->add(new DateInterval("P1W")); //P1W means an interval of one week.
 
echo $date->format('Y-M-d'); //2022-Aug-31

Perfect! By the way, ‘P1W’ represents an offset of 7 days – a week. 

Let’s make a leap to next month now and see how PHP adds a month to date.

Get Next Month Date in PHP DateTime

To jump to the next month from the current date, let’s use the good old modify method.

$date = new DateTime("now");
 
$date->modify('+1 month');
 
echo $date->format('Y-M-d'); //2022-Sep-24

Cool! This seems to be working, but there’s a caveat. PHP adds 1 month to date – a fixed 31 day offset, which could be confusing sometimes. For instance, a month added to 2022-Aug-31 should give us the last day of next month, 2022-Sep-30.

But adding a fixed offset of 31 days would skip past that date, as shown.

$date = new DateTime("2022-08-31");
 
$date->modify('+1 month');
 
echo $date->format('Y-M-d'); //2022-Oct-01
 

See! There are many clever ways to tackle this, but the simplest is what we have found.

$date = new DateTime("2022-08-31");
 
$date->modify('last day of next month');
 
echo $date->format('Y-M-d'); //2022-Sep-30
 

Changing the modifier to a relative format saves us big time! 

So, I guess we are good to go for creating date formats in PHP. 

Conclusion

Wrap Up

This article explores how to create date format in PHP. The article features sections on how to get current, tomorrow, yesterday, next week, and month dates in PHP. All the examples use pretty much the same methods of the DateTime function. The article also highlights an important caveat when PHP adds a month to date.

Hopefully, you’ve enjoyed this article. Stay tuned for more at FuelingPHP.

PHP Date Classes & Functions Mentioned

DateTime: (PHP 5 >= 5.2.0, PHP 7, PHP 8) The DateTime class in PHP represents dates/times and features many useful functions to easily manipulate the objects. It was included in PHP 5.2.0 and remained in use. Given its utility and stability, the depreciation seems unlikely.

DateTimeZone: (PHP 5 >= 5.2.0, PHP 7, PHP 8) The DateTimeZone class in PHP represents a time zone. It has been included in PHP from the fifth version and is available in the latest PHP version. The class seems stable and reliable, and the probability of depreciation seems low.

DateInterval: (PHP 5 >= 5.3.0, PHP 7, PHP 8) The DateInterval helps in storing a fixed time interval. Given its use and long-term availability, depreciation seems unlikely in the future.

Want to learn more about PHP?

We have many fun articles related to PHP. You can explore these to learn more about PHP.


© 2022 Confident.Systems