DJ Mike's Tutorials: PHP

Time And Dates pt.3


< ^ >

mktime()

time() and date() give you ways to work with the current time and date. mktime() gives you a way to work with almost any time and dates from 1970 to 2038 if your server is before PHP 5.1.0. After PHP 5.1.0, the valid range is from 1901 to 2038

mktime() returns a timestamp and has six optional arguments: hour, minute, second, month, day of month and year. Arguments may be left out in order from right to left; any arguments left out will be set to the current time and/or date. For example, if you want to find out what day of the week Christmas falls on in 2020, you can use mktime() to get a time stamp for 6:00 AM, 12/25/2005, use getdate() to convert the timestamp into an array then use the key, weekday to get the day of the week:



<?
$xmas 
mktime(6,0,0,12,25,2020);
$xmas getdate($xmas);
echo 
"Christmas 2020 falls on $xmas[weekday]"
?>


Christmas 2020 falls on Friday

strtotime()

strtotime() is a function that can convert a string of English words into a timestamp. For example, to get a timestamp for next friday, all you have to do is use strtotime("next friday"), assign it to a variable, use date() to format it into a string and echo it:



<?
$next_friday 
strtotime("next friday");
$next_friday date("F jS Y"$next_friday);
echo 
"Next friday falls on $next_friday";
?>


Next friday falls on February 10th 2012



Words and abbreviations recognized by strtotime()
Month names Full name or abbreviations
Days of the week Full name or abbreviations
Time units Year, month, fortnight, week, day, hour, minute, second, am, pm
Plus or minus + or -
All numbers  
Time zone gmt, pdt, akst etc.

checkdate()

checkdate() is used to validate dates. It accepts three integers as arguments; month, day and year and returns true if the numbers represent a valid date between the year 0 and 32767 and false if it is not. If letters are used as an argument, checkdate() will generate a PHP error message. You can suppress that error message by appending a @ to checkdate(). Below is an example of a form using checkdate() to validate the input.

Example | Source Code



Example | Source Code

< ^ >



Created by DJ Mike from Santa Barbara

DJ Mike


Dance Away Santa Barbara's Home Page
<a href="http://www.statcounter.com/" target="_blank"> <img src="http://c5.statcounter.com/counter.php?sc_project=1321035&java=0&security=da2193dc" alt="counter free hit invisible" border="0" /></a>