PHP DateTime - Create, Format and Compare Dates with PHP
The DataTime
object was released in PHP 5.2.0 and provides a more robust way of working with dates in PHP. It can be used to create new dates and format them and replaces the need to use separate date functions such as strtotime()
, time()
and date()
.
In this tutorial, we will learn how to create a date in PHP, format it in various ways and compare different DateTime
objects.
Create a New Date with DateTime
Let's create a new date with DateTime
, which will be based on the current time on your machine and will contain hours, months, days, hour, minutes and seconds.
$date = new DateTime();
echo $date->format('Y-m-d H:i:s');
2020-09-05 19:40:23
Now we know how to create a DateTime
object we can use any of the DateTime methods to work with it.
DateTime Methods
Here are all the main methods available in PHP DateTime for reference.
format
- format a date for printing.add
- adds a number of days, months, years, hours, minutes and seconds to a DateTime objectsetTimezone
- set a timezone in the formatTimezone/Local
setTimestamp
- set a timestamp to convert into a DateTime objectsetTime
- reset the current DateTime object time to a new timecreateFromFormat
- parses a time string according to a specified formatcreateFromImmutable
- creates a mutable DateTime object from an immutable onemodify
- increment or decrement the date in seconds, minutes, hours, days, weeks, months or yearssetDate
- resets the current date of the DateTime object to a different datesetISODate
- resets the current date according to ISO 8601 standardssub
- subtract from the date in seconds, minutes, hours, days, weeks, months or yearsdiff
- returns the difference between two DateTime objectsgetOffset
- returns a timezone offsetgetTimestamp
- get a Unix timestamp from the dategetTimezone
- get timezone relative to current DateTime
Formatting Dates
You can format a date using any combination of the available DateTime format characters. Let's create a human-friendly date using the format
method.
$date = new DateTime();
echo $date->format('D, dS M Y');
Sat, 05th Sep 2020
Adding and Subtracting Time from a Date
If you need to add or subtract, seconds, minutes, hours, days, weeks or years with a date you can do this using the DateTime
modify
method. Let's add 1
day to a date.
$date = new DateTime('2020-09-09');
$date->modify('+1 day');
echo $date->format('Y-m-d H:i:s');
2020-09-10 00:00:00
Here is another example, this time subtracting 2
weeks from the date.
$date = new DateTime('2020-09-09');
$date->modify('-2 weeks');
echo $date->format('Y-m-d H:i:s');
2020-08-26 00:00:00
note - you must pass in the format
method before printing the date.
Get Tomorrows Date in PHP
To get tomorrows date using DateTime
we could do something like this.
$date = new DateTime();
$date->modify('+1 day');
echo $date->format('Y-m-d H:i:s');
2020-09-06 20:12:20
Create a Date in a Timezone
To create a new DateTime
in a timezone, create a new DateTimeZone
in the second parameter of DateTime
.
$date = new DateTime('', new DateTimeZone('Europe/London'));
echo $date->format('Y-m-d H:i:s');
2020-09-05 21:31:42
To set a new timezone to your DateTime
object, use the setTimezone
method.
$date->setTimezone(new DateTimeZone('America/New_York'));
echo $date->format('Y-m-d H:i:s');
2020-09-05 16:36:01
Create a Date from a Timestamp
If you have a timestamp you need to convert to a useable date you can pass it into the setTimestamp
method. Then you can format it.
$date = new DateTime();
$date->setTimestamp('1599340962');
echo $date->format('Y-m-d H:i:s');
2020-09-05 21:22:42
Comparing Dates
DateTime
objects can be compared using if statements. In the example below we are creating two DateTime
objects, one is my local time ($date
) and the other is in New York ($date_new_york
). When comparing the dates to see if one is greater we should find that New York time is greater than my local time.
$date = new DateTime();
$date_new_york = new DateTime('', new DateTimeZone('America/New_York'));
if ($date < $date_new_york) {
echo 'local time '. $date->format('Y-m-d H:i:s') .' is less than New York time';
}
local time 2020-09-05 21:51:22 is less than New York time
You can, of course, use any of the available PHP comparison operators to make DateTime
comparisons.
Conclusion
You know now how to create, format and compare dates in PHP using the DateTime
object. If you just need to create a date then the date()
function might still be the cleanest option, however, for many other purposes, DateTime
has much more powerful functionality.