Using time in php is very simple. All you need to remember is a date and a set of 3 functions as follows:
Date:
1st of January, 1970.
Functions:
1) time();
2) strftime();
3) strtotime();
I will explain two codes in this tutorial. First is to get the current date or time and the second to save and get the time from the mysql database and format it the way we like.
Code 1:
<?php
$now = time();
echo strftime("%d %m %Y", $now);
?>
Result:
2011-01-01
(Note: If this page is run on January 01, 2011.)
Explaination:
The time() function returns the current Unix timestamp, i.e., seconds since Januart 1, 1970.
So, if this page is run on January 01, 2011, the value the time() function will return 1293840000. This value is saved in $now variable.
Then in the next line we can format this timestamp by using strftime function. We pass two parameters in this function, a string which will be returned and the unix timestamp which is actually $now. The string is returned as it is, but the values written with % sign are replaced with the format of time. For instance %Y will be replaced with the YYYY format. In this case here the %Y is 2011.
php programming tutorials
ReplyDelete