php date

php relative datetime formats

Add 1 year to date:

<?php
$old_date = '2013-04-24 09:31:46';
$old_date_seconds = strtotime( $old_date );
$new_date_seconds = $old_date_seconds + 31536000; // +1 year
$new_date = date( 'Y-m-d H:i:s', $new_date_seconds );
?>

Current date and time with php:

<?php
echo date("Y-m-d F H:i:s"); // 2012-05-28 May 17:33:21
echo date("l jS \\of F Y h:i:s A"); // Monday 28th of May 2012 17:33:58 AM
?>

Relative date and time with php:

<?php

date("Y-m-d F H:i:s", strtotime("Today")); // current date
date("Y-m-d F H:i:s", strtotime("now")); // current date
date("Y-m-d F H:i:s", strtotime("10 September 2000"));
date("Y-m-d F H:i:s", strtotime("+1 day"));
date("Y-m-d F H:i:s", strtotime("+1 week"));
date("Y-m-d F H:i:s", strtotime("+ 1 years 1 week 2 days 4 hours 2 seconds"));

date("Y-m-d F H:i:s", strtotime("next Thursday"));
date("Y-m-d F H:i:s", strtotime("last Monday"));

// on 2/8/2010
date("Y-m-d F H:i:s", strtotime("first day")); # 02/01/10
date("Y-m-d F H:i:s", strtotime("last day")); # 02/28/10
date("Y-m-d F H:i:s", strtotime("last day next month")); # 03/31/10
date("Y-m-d F H:i:s", strtotime("last day last month")); # 01/31/10
date("Y-m-d F H:i:s", strtotime("2009-12 last day")); # 12/31/09 - this doesn't work if you reverse the order of the year and month
date("Y-m-d F H:i:s", strtotime("2009-03 last day")); # 03/31/09
date("Y-m-d F H:i:s", strtotime("2009-03")); # 03/01/09
date("Y-m-d F H:i:s", strtotime("last day of march 2009")); # 03/31/09
date("Y-m-d F H:i:s", strtotime("last day of march")); # 03/31/10

date("Y-m-d F H:i:s", strtotime("next month"));
date("Y-m-d F H:i:s", strtotime("first day of next month"));
date("Y-m-d F H:i:s", strtotime("first day of last month"));
date("Y-m-d F H:i:s", strtotime("first day of +1 month"));
date("Y-m-d F H:i:s", strtotime("- 1 month"));
date("Y-m-d F H:i:s", strtotime("Monday this week"));
date("Y-m-d F H:i:s", strtotime("midnight first day of last monty"));
date("Y-m-d F H:i:s", strtotime("+1 months")); // first day of the next month
date("Y-m-d F H:i:s", strtotime("last Saturday"));
date("Y-m-d F H:i:s", strtotime("last Saturday-1 week"));
date("Y-m-d F H:i:s", strtotime("midnight +1 day"));
date("Y-m-d F H:i:s", strtotime("midnight next monday"));
date("Y-m-d F H:i:s", strtotime("14:00 yesterday"));
date("Y-m-d F H:i:s", strtotime("last day of next month"));
date("Y-m-d F H:i:s", strtotime("noon"));
date("Y-m-d F H:i:s", strtotime("midnight"));
date("Y-m-d F H:i:s", strtotime("10am"));
date("Y-m-d F H:i:s", strtotime("2pm"));
date("Y-m-d F H:i:s", strtotime("1st jan"));
date("Y-m-d F H:i:s", strtotime("first monday january 2010"));
date("Y-m-d F H:i:s", strtotime("last friday"));
date("Y-m-d F H:i:s", strtotime("next monday"));
date("Y-m-d F H:i:s", strtotime("first monday may 2010"));
date("Y-m-d F H:i:s", strtotime("first Thursday"));
date("Y-m-d F H:i:s", strtotime("last monday june 2010"));
date("Y-m-d F H:i:s", strtotime("next day"));
date("Y-m-d F H:i:s", strtotime("+0 week sun nov 2009")); // first sunday in nov 2009 
date("Y-m-d F H:i:s", strtotime("+1 week sun nov 2009")); // second sunday
date("Y-m-d F H:i:s", strtotime("-1 week sun nov 2009")); // last sunday in oct 2009
date("Y-m-d F H:i:s", strtotime("2010-W10-1")); // returns the date of monday in week
date("Y-m-d F H:i:s", strtotime("2010-W10-7")); // returns the date of sunday in week
date("Y-m-d F H:i:s", strtotime("second wednesday"));
date("Y-m-d F H:i:s", strtotime("third sunday"));
date("Y-m-d F H:i:s", strtotime("1 day 2 hours ago"));
date("Y-m-d F H:i:s", strtotime("-1 day 2 hours")); // same as "-1 day +2 hours"
date("Y-m-d F H:i:s", strtotime("-1 day -2 hours"));
date("Y-m-d F H:i:s", strtotime("Monday"));
date("Y-m-d F H:i:s", strtotime("Monday"));

date("Y-m-d F H:i:s", strtotime("last monday", strtotime("15 July 2005"))); // 11th 
date("Y-m-d F H:i:s", strtotime("this monday", strtotime("15 July 2005"))); // 18th 
date("Y-m-d F H:i:s", strtotime("next monday", strtotime("15 July 2005"))); // 25th 

?>

Leave a Comment