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() month pretty much handy while you are dealing with date/time functionality in PHP. for example
this function would print current date in simple day-month-year format as shown above.
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]
It would print false as output because 20th doesnt exist. or does it?
There’s a date_create() function to create an date object so date/time funcsionality.
[php]$date = date_create();
Output: object(DateTime) 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() 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);
Output: object(DateInterval) 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() is a function to format date as provided.
time() function gives you current epoch or unix time.
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
function returns relative time,string unix timestamp format.
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 1467738028 1468314028 1468598433 1467928800
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
And the output would be something like this
Output: 4 weeks ago
- web development
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.