Free cookie consent management tool by TermsFeed Generator PHP Date and Time | Amir Kamizi
AMIR KAMIZI
Home Blog Courses Books Newsletter Store Membership Buy me a coffee
PHP Date and Time

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

https://youtu.be/-jkzOI_VXLI

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

Category: programming

Tags: #php

Join the Newsletter

Subscribe to get my latest content by email.

I won't send you spam. Unsubscribe at any time.

Related Posts

How to use Git Alias to increase your productivity (video)
Nov 27, 2022 programming

How to use Git Alias to increase your productivity (video)

Today we are going to talk about Git Alias and how to increase your productivity by adding shorter aliases for commands that we use everyday. ...

8 Min Read Read More
the ultimate guide to Git: learn everything you need in less than 1 hour
Nov 28, 2022 programming

the ultimate guide to Git: learn everything you need in less than 1 hour

Here is everything you need to know about git in a series of short question and answers. ...

11 Min Read Read More
PHP function
Mar 21, 2023 programming

PHP function

Today we are going to learn about functions in PHP. Functions are very important because they can help you divide your application in small bites that can be handled with a function. ...

10 Min Read Read More
PHP Abstract Class
Mar 22, 2023 programming

PHP Abstract Class

Today we are going to talk about abstract classes in PHP. Abstract class is another important topic in object oriented programming and today we are going to learn everything about them. ...

8 Min Read Read More

Recommended Courses

Introduction to Machine Learning in PHP

Introduction to Machine Learning in PHP

Learn to Build Different Machine Learning Models Easily ...

PHP Tutorial Beginner to Advanced

PHP Tutorial Beginner to Advanced

Learn everything you need to start a successful career as a PHP developer ...