PHP Date and Time
Last Updated on Mar 21, 2023
Your Two Friends
date()
strtotime()
If you have to do something related to date and time these 2 functions are your friends. Now let’s see how they work.
strtotime()
This function gets a string as argument. The argument can be:
25 aug 2021
25 august 2021
aug 25 2021
2020-08-25
08/25/2021
or even a more descriptive text like:
7 days ago
7 years ago
7 years 2 months 24 days ago
This function returns a timestamp
What is a timestamp? It’s a number
It’s the number of seconds from 1st of january 1970.
That timestamp can be formatted to a date later or can be used to do calculations.
Let’s see some examples:
echo strtotime("today");
// 1637362800
echo strtotime("yesterday");
// 1637276400
echo strtotime("tomorrow");
// 1637449200
echo strtotime("last week");
// 1636351447
echo strtotime("last month");
// 1634706247
echo strtotime("next tuesday");
// 1637622000
echo strtotime("3 days ago");
// 1637129047
echo strtotime("2 months ago");
// 1632114247
echo strtotime("2 year 2 months 2 days ago");
// 1568783047
echo strtotime("+3 weeks");
// 1639202647
echo strtotime("-3 weeks");
// 1635570247
echo strtotime("27 aug 2022");
// 1661551200
echo strtotime("27 august 2022");
// 1661551200
echo strtotime("aug 27 2022");
// 1661551200
echo strtotime("2022-08-27");
// 1661551200
echo strtotime("08/27/2022");
// 1661551200
date()
For the first argument it gets the formatting of the date.
For example 'Y-m-d' is the standard ISO format for dates 2021-11-14
The second argument is a timestamp. If you don’t specify it, you'll get the current date and time with the formatting you want.
For example
date('Y-m-d') will give me today which is 2021-11-22 But if you specify a date like date('Y-m-d',strtotime('2 days ago')); you will get 2021-11-20
date('Y-m-d');
// today (2021-11-22)
date('Y-m-d',strtotime('2 days ago'));
// 2021-11-20
Let’s change the formatting of those timestamps we saw in strtotime() and see their outputs
<?php
echo date('Y-m-d',strtotime("today"));
// 1637362800 -> 2021-11-20
echo date('Y-m-d',strtotime("yesterday"));
// 1637276400 -> 2021-11-19
echo date('Y-m-d',strtotime("tomorrow"));
// 1637449200 -> 2021-11-21
echo date('Y-m-d',strtotime("last week"));
// 1636351447 -> 2021-11-08
echo date('Y-m-d',strtotime("last month"));
// 1634706247 -> 2021-10-20
echo date('Y-m-d',strtotime("next tuesday"));
// 1637622000 -> 2021-11-23
echo date('Y-m-d',strtotime("3 days ago"));
// 1637129047 -> 2021-11-17
echo date('Y-m-d',strtotime("2 months ago"));
// 1632114247 -> 2021-09-20
echo date('Y-m-d',strtotime("2 year 2 months 2 days ago"));
// 1568783047 -> 2019-09-18
echo date('Y-m-d',strtotime("+3 weeks"));
// 1639202647 -> 2021-12-11
echo date('Y-m-d',strtotime("-3 weeks"));
// 1635570247 -> 2021-10-30
echo date('Y-m-d',strtotime("27 aug 2022"));
// 1661551200 -> 2022-08-27
echo date('Y-m-d',strtotime("27 august 2022"));
// 1661551200 -> 2022-08-27
echo date('Y-m-d',strtotime("aug 27 2022"));
// 1661551200 -> 2022-08-27
echo date('Y-m-d',strtotime("2022-08-27"));
// 1661551200 -> 2022-08-27
echo date('Y-m-d',strtotime("08/27/2022"));
// 1661551200 -> 2022-08-27
Date Formats
here is a list of formats you can have as the first argument.
<?php
// today is 2021-11-20 at 10:15:50
// The day of the month (from 01 to 31)
echo date('d'); // 20
// The day of the month (three letters)
echo date('D'); // Sat
// The day of the month without leading zeros (1 to 31)
echo date('j'); // 20
// (lowercase 'L') The day of the month full text
echo date('l'); // Saturday
// The ISO-8601 numeric representation of a day (1 for Monday, 7 for Sunday)
echo date('N'); // 6
// The English ordinal suffix for the day of the month (2 characters st, nd, rd or th. Works well with j)
echo date('S'); // th
// A numeric representation of the day (0 for Sunday, 6 for Saturday)
echo date('w'); // 6
// The day of the year (from 0 through 365)
echo date('z'); // 323
// The ISO-8601 week number of year (weeks starting on Monday)
echo date('W'); // 46
// A full textual representation of a month (January through December)
echo date('F'); // November
// A numeric representation of a month (from 01 to 12)
echo date('m'); // 11
// A short textual representation of a month (three letters)
echo date('M'); // Nov
// A numeric representation of a month, without leading zeros (1 to 12)
echo date('n'); // 11
// The number of days in the given month
echo date('t'); // 30
// Whether it's a leap year (1 if it is a leap year, 0 otherwise)
echo date('L'); // 0
// The ISO-8601 year number
echo date('o'); // 2021
// A four digit representation of a year
echo date('Y'); // 2021
// A two digit representation of a year
echo date('y'); // 21
// Lowercase am or pm
echo date('a'); // am
// Uppercase AM or PM
echo date('A'); // AM
// Swatch Internet time (000 to 999)
echo date('B'); // 427
// 12-hour format of an hour (1 to 12)
echo date('g'); // 10
// 24-hour format of an hour (0 to 23)
echo date('G'); // 10
// 12-hour format of an hour (01 to 12)
echo date('h'); // 10
// 24-hour format of an hour (00 to 23)
echo date('H'); // 10
// Minutes with leading zeros (00 to 59)
echo date('i'); // 15
// Seconds, with leading zeros (00 to 59)
echo date('s'); // 50
// The timezone identifier (Examples: UTC, GMT, Atlantic/Azores)
echo date('e'); // Europe/Berlin
// (capital i) Whether the date is in daylights savings time (1 if Daylight Savings Time, 0 otherwise)
echo date('I'); // 0
// Difference to Greenwich time (GMT) in hours (Example: +0100)
echo date('O'); // +0100
// Difference to Greenwich time (GMT) in hours:minutes (added in PHP 5.1.3)
echo date('P'); // +01:00
// Timezone abbreviations (Examples: EST, MDT)
echo date('T'); // CET
// Timezone offset in seconds. The offset for timezones west of UTC is negative (-43200 to 50400)
echo date('Z'); // 3600
// The ISO-8601 date (e.g. 2013-05-05T16:34:42+00:00)
echo date('c'); // 2021-11-20T10:15:50+01:00
// The RFC 2822 formatted date (e.g. Fri, 12 Apr 2013 12:01:05 +0200)
echo date('r'); // Sat, 20 Nov 2021 10:15:50 +0100
// The seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
echo date('U'); // 1637399750
Conclusion
Now you know about date and times in PHP.
I recommend you to open a PHP file and write dates in different formats.
If you have any suggestions, questions, or opinions, please contact me. I’m looking forward to hearing from you!
Key takeaways
- working with dates
- working with times
- formating dates