PHP Date and Time Reference | Cheatsheet


Why Do I Need PHP Date And Time Refernce?

Need for a PHP date and time reference arose when I was working on a blog and I had to check for the date and time functions every now and then. Let me admit something here. Nobody is a perfect programmer. We usually need references and cheatsheets to get out word done quickly. PHP like every other maintained programming language has like thousands if not millions of function names and parameters.
I cannot remember all the functions and classes for a specific task. I simply needed an online reference for PHP date time functions so that I dont have to go snoop around like 30 pages to find one itsy bitsy function.

date() Function

date() month pretty much handy while you are dealing with date/time functionality in PHP. for example

[php]print date("d-m-Y");[/php]

Output:  05-07-2016

this function would print current date in simple day-month-year format as shown above.


Output:  10:37:58

and this function would print time in mundane hour:minutes:seconds format as given below

You can also add other details like day and am/pm as well.

[php]print date("l d-m-Y h:i A");[/php]

Output:  Tuesday 05-07-2016 10:44 AM

If you wanted to check if a date exists or not, you can use checkdate(‘m’,’d’,’Y’) function
It works something like this

[php]$check = (checkdate(20, 10, 1985)) ? "TRUE" : "FALSE";
print $check; [/php]

Output:  FALSE

It would print false as output because 20th doesnt exist. or does it?

date_create() Function

There’s a date_create() function to create an date object so date/time funcsionality.

[php]$date = date_create();

Output:  object(DateTime)[1]
public 'date' => string '2016-07-02 09:06:20' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'Europe/Paris' (length=12)


date_diff() Function

date_diff() is a function that gives out the interval between two given dates.


$date1 = date_create("2013-03-15");
$date2 = date_create("2013-12-12");
$diff = date_diff($date1,$date2);
var_dump($diff) ;

Output:  object(DateInterval)[3]
public 'y' => int 0
public 'm' => int 8
public 'd' => int 27
public 'h' => int 0
public 'i' => int 0
public 's' => int 0
public 'weekday' => int 0
public 'weekday_behavior' => int 0
public 'first_last_day_of' => int 0
public 'invert' => int 0
public 'days' => int 272
public 'special_type' => int 0
public 'special_amount' => int 0
public 'have_weekday_relative' => int 0
public 'have_special_relative' => int 0


date_format() Function

date_format() is a function to format date as provided.

print date_format($date,"d-m-Y");[/php]

Output:  30-07-1999


time() Function

time() function gives you current epoch or unix time.

[php]print time();[/php]

Output:  1467707548


gmdate() Function

You can format the output in human reabible using gmdate() function

$unixTime = time();
print gmdate(‘l h:i:s d-m-Y’, $unixTime);

Output:  Tuesday 10:09:13 05-07-2016


strtotime Function

function returns relative time,string unix timestamp format.

[php]print strtotime("now");
print strtotime("12 December 2009");
print strtotime("+8 hours");
print strtotime("+1 week");
print strtotime("+1 week 3 days 7 hours 5 seconds");
print strtotime("next Friday");[/php]


Output:  1260572400


Relative Time Function

Here’s one script I have found on github to find relative time.

Just copy above above function and then you can use it in your program like

[php]print time2str(strtotime("10-june-2016"));[/php]

And the output would be something like this

Output:  4 weeks ago


You can reach Waqas Yousaf through twitter @wiqi.

One Comment:

  1. Woah! I’m really loving the template/theme of this website.
    It’s simple, yet effective. A lot of times it’s very difficult to get that “perfect balance” between superb usability and visual appeal.
    I must say that you’ve done a superb job with this.

    Additionally, the blog loads super fast for me on Firefox.

    Outstanding Blog!

Leave a Reply

Your email address will not be published. Required fields are marked *