A web developer's blog. PHP, MySQL, CakePHP, Zend Framework, Wordpress, Code Igniter, Django, Python, CSS, Javascript, jQuery, Knockout.js, and other web development topics.

How to get the a date before current date in PHP

Given 2008-01-31 as the current date and the value of “$days_before” is 2, the code will echo 2008-01-29. The date 2 days before the current date.

$days_before = 2;
$current_date = date('Y-m-d');
$str = strtotime($current_date)-(86400*$days_before); //1 day has 86400
echo date('Y-m-d',$str);
This entry was posted in General and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>