Working with dates and times in PHP - Making your own date and time (Page 3 of 4 ) Are you satisfied now that you can show your user for the current date and time? If you do, then don’t need to read this article for more. The next step is for people who need something more. The next function that we have to understand is mktime(). This function will make your own timestamp from a given set of variables that you pass to the function. int mktime ( int hour, int minute, int second, int month, int day, int year [, int is_dst]) |
Now, I want to know in what day my birthday was on. My birth date is July 21st 1974. I’ll use the mktime function like so: <?php $mybirthdate = mktime(0,0,0,7,21,1974); print date("l", $mybirthdate); ?> |
Wow, I finally found out that my birthday is on Sunday. :). Now the next case I want to know is what day is 25 days from now. Let’s do it this way: <?php $next25day = mktime(0,0,0,date("n"),date("j")+25,date("Y")); print date("l", $next25day); ?> |
Give attention to the variables that I passed to the mktime function. I’ve passed date("j")+25. It means that I want to add 25 to the current date. You can figure out what it will produce. Another method This is another method to access the current date or the given timestamp that you’ve produced by your own script. The method, or you can call it function, is getdate(). This function will produce an associative array containing the date information. The array key is: "seconds" - seconds "minutes" - minutes "hours" - hours "mday" - day of the month "wday" - day of the week, numeric: from 0 as Sunday up to 6 as Saturday "mon" - month, numeric "year" - year, numeric "yday" - day of the year, numeric; i.e. "299" "weekday" - day of the week, textual, full; i.e. "Friday" "month" - month, textual, full; i.e. "January" |
Example Usage: <?php $today = getdate(); print $today["month"] // it will print the month of current date print $today["weekday"] // it will print the day of current date ?> |
Next: My own Language >>
More Miscellaneous Articles More By Hermawan Haryanto |