We use cookies to keep our site relevant and easy to use, your continued use of this site is consent that we may set several cookies (see our Privacy & Cookie Policy), click to always allow cookies from our site (and not see this notifcation on your next visit) or read more.Allow Cookies

EU legislation requires that all websites clearly specify if cookies are being used and their purpose, You can read more about how we use cookies (and which cookies we use) in our Privacy and Cookie Policy.

You will see this notification the first time you visit our website unless you accept cookies (in which case we'll set a cookie to remember thay you're happy for us to to set cookies!).

Useful PHP mktimes

Creating – yesterday, tomorrow, last month etc. in PHP time can be a little time consuming – here are some quick cut and pastes!

< ? $yesterday = mktime(0,0,0,date("m"),date("d")-1,date("Y")); $today = mktime(0,0,0,date("m"),date("d"),date("Y")); $tomorrow = mktime(0,0,0,date("m"),date("d")+1,date("Y")); $lastmonth = mktime(0,0,0,date("m")-1,date("d"),date("Y")); $nextmonth = mktime(0,0,0,date("m")+1,date("d"),date("Y")); $lastyear = mktime(0,0,0,date("m"),date("d"),date("Y")-1); $nextyear = mktime(0,0,0,date("m"),date("d"),date("Y")+1); // The "t" below gives the number of days in a given month. $lastday = mktime(0,0,0,date("m"),date("t"),date("Y")); $yesterday = strftime("%A %B %d, %Y",$yesterday); $today = strftime("%A %B %d, %Y",$today); $tomorrow = strftime("%A %B %d, %Y",$tomorrow); $lastmonth = strftime("%A %B %d, %Y",$lastmonth); $nextmonth = strftime("%A %B %d, %Y",$nextmonth); $lastyear = strftime("%A %B %d, %Y",$lastyear); $nextyear = strftime("%A %B %d, %Y",$nextyear); $lastday = strftime("%A %B %d, %Y",$lastday); echo "Yesterday was: $yesterday"; echo "Today is: $today";
echo "Tomorrow is: $tomorrow";
echo "Last Month was: $lastmonth";
echo "Next Month is: $nextmonth";
echo "Last Year was: $lastyear";
echo "Next Year is: $nextyear";
echo "Last day of the month is: $lastday";
?>

This entry was posted on Tuesday, August 1st, 2006 at 7:19 am and is filed under Programming, Software. You can follow any responses to this entry through the RSS 2.0 feed.


Comments are closed.