Let’s get you a “date” on php

Hey fellows! How you doin! Today we want to talk about dates on php.
But before that we should know about GMT timezone and Unix Timestamp.

GMT timezone : it’s that the time of Greenwich is the center of the world clock , and the time of every other place is measured comparing to Greenwich . for example time in Tehran  is GMT+3:30 which means it’s 3:30 h ahead of the time of Greenwich .

if you’re curious  about time stuff (only if you’re interested ) check the pages below :

How earth moves 
What is the Prime Meridian and why is it in Greenwich?

Unix timestamp : The unix time stamp is a way to track time as a running total of seconds. This count starts at the Unix Epoch on January 1st, 1970 at UTC. Therefore, the unix time stamp is merely the number of seconds between a particular date and the Unix Epoch. It should also be pointed out (thanks to the comments from visitors to this site) that this point in time technically does not change no matter where you are located on the globe. This is very useful to computer systems for tracking and sorting dated information in dynamic and distributed applications both online and client side.[1]

So what time is it now ?

there are plenty functions to get the current time but let’s start with  time()  the most common one  

time() : returns the unix timestamp of the current time
if you run time() , the output will be a bunch of numbers which we know
is the unix timestamp . so if we want to convert these numbers to the date
format we want , we call the date() function .

date( format , timestamp )Format a local date and time and return the formatted date strings ( if you use gmdate() it returns the formatted date in Greenwich time)

[code language=”php”]
echo date(“l – M d h:i:s A”,time()).”\n”;
echo gmdate(“l – M d h:i:s A”,time());
[/code]

The output :
Friday - Sep 28 07:28:26 PM
Friday - Sep 28 05:28:26 PM

dateformat

Note : if the output time is not time of your computer(server) , open php.ini . Look for the following: date.timezone = "Europe/Warsaw". Probably You have different value than my Europe/Warsaw. So search just string: date.timezone.
Change value Europe/Warsaw to the proper value, for example date.timezone = "Asia/Tehran"

But how we get the time of another time let’s say 23th of  July 1850

mktime(hour,minute,second,day,month,year
returns the unix timestamp of the given date

[code language=”php”]
echo mktime(0,0,0,7,23,1950).”\n”;
echo date (“l – F d ,Y “,mktime(0,0,0,7,23,1950));
[/code]

The output :
 -613616400
Sunday - July 23 ,1950

if you want a more precise time , you should use microtime(timestamp [default time()] )
Return the current Unix timestamp with microseconds

 

[code language=”php”]
echo microtime();
[/code]

The output :
0.22602500 153815394

the other way to get a time is to use getdate() ( if only we could use this function in our real life :)) )

getdate(timestamp [default time()]) : returns Return date/time information of the current local date/time in an array

[code language=”php”]
print_r (get_date(mktime(0,0,0,7,23,1950)));
[/code]

The output :
Array ( [seconds] => 0 [minutes] => 0 [hours] => 0 [mday] => 23 [wday] => 0 [mon] => 7 [year] => 1950 [yday] => 203 [weekday] => Sunday [month] => July [0] => -613616400 )

 

and the last one we will talk about is a fun function ! strtotime() ! you can say “ok php what’s the date of now , next week , 5 days later ,…”

strtotime(time , nowParse English textual datetimes into Unix timestamps

[code language=”php”]
$now = strtotime(“now”);
echo $now .” | “.date(“l – M d h:i:s A”,$now).”
“;
$octo = strtotime(“3 October 2005″);
echo $octo .” | “.date(“l – M d h:i:s A”,$octo).”
“;
$fivehours = strtotime(“+5 hours”) ;
echo $fivehours .” | “.date(“l – M d h:i:s A”,$fivehours).”
“;;
$week = strtotime(“+1 week 3 days 7 hours 5 seconds”);
echo $week .” | “.date(“l – M d h:i:s A”,$week).”
“;
[/code]

The output :
1538156305 | Friday - Sep 28 07:38:25 PM
1128290400 | Monday - Oct 03 12:00:00 AM
1538174305 | Saturday - Sep 29 12:38:25 AM
1539045510 | Tuesday - Oct 09 02:38:30 AM

Done ! Leave us comments ! please :))

2 thoughts on “Let’s get you a “date” on php

Leave a Reply

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