-1

Whats the best method to find the date in date('m.d.Y') format that is 1 day ago from another date in date('m.d.Y') format in PHP? Must be applicable to v5.1.6. I need to build a recursive method to iterate through previous days.

Thanks!

------------Revision -------

$date = date('m.d.Y');
$date = date('m.d.Y', strtotime($date .' -1 day'));

The doesn't seem to work :( The date must begin and end in the same format 'm.d.Y'

user2799603
  • 873
  • 3
  • 13
  • 19

3 Answers3

2

You really should be looking at using DateTime, DateInterval, and DatePeriod classes for this. As an example:

$start_date = '12.31.2013';  // your input date
$start_date_time = DateTime::createFromFormat('m.d.Y', $start_date);
$one_day_interval = new DateInterval('P1D');

// iterate X number of days example
$iteration_days = 30; // some value for number of iterations you want
$count_period = new DatePeriod($start_date_time, $one_day_interval, $iteration_days);

foreach($count_period as $day) {
    // do something
}

// iterate between start and end date example
$end_date = '3.31.2014';
$end_date_time = DateTime::createFromFormat('m.d.Y', $end_date);

$date_period = new DatePeriod($start_date_time, $one_day_interval, $end_date_time);

foreach($date_period as $day) {
    // do something
}
Mike Brant
  • 70,514
  • 10
  • 99
  • 103
  • Thanks! But is there something simpler? I have my recursive function all setup, just need to e.g. get yesterday from today date? – user2799603 Feb 28 '14 at 17:55
  • @user2799603 That is pretty simple and is just an example. You can easily create a DateTime object and modify it's date (like if you want to iteratively change the object using something like `$date_time->modify('-1 day')` I mentioned the DatePeriod object as I imagine that you will need to have some way of breaking out of your recursive function call (i.e. some end date, or X number of iterations), so you might be able to eliminate recursion altogether. – Mike Brant Feb 28 '14 at 18:10
  • Ok, so after $date_time->modify('-1 day'); how do i convert it back to string? – user2799603 Feb 28 '14 at 18:12
  • @user2799603 Read the DateTime documentation. It has everything you need including the the `format()` method that would produce formatted string output. – Mike Brant Feb 28 '14 at 18:14
  • I see. However, I actually have PHP 5.1.6, and I don't believe DateTime is supported according to http://www.php.net/manual/en/class.datetime.php – user2799603 Feb 28 '14 at 18:17
  • @user2799603 Wow. You should get on a newer version of PHP. Your version of PHP is like 8 years old. That is ANCIENT in computer time. – Mike Brant Feb 28 '14 at 18:19
  • I know... but I can't... alternative would be ideal – user2799603 Feb 28 '14 at 18:20
  • @user2799603 You will likely need to go with something like what Awlad Liton posted. – Mike Brant Feb 28 '14 at 18:42
1
$date = DateTime::createFromFormat('m.d.Y', $yourDate);
$date->sub(new DateInterval('P1D');
MueR
  • 977
  • 7
  • 13
0

try like this:

   $date =date("Y-m-d"); //set your date
   echo date('m.d.Y', strtotime($date .' -1 day'));

demo : https://eval.in/107144

Awlad Liton
  • 9,366
  • 2
  • 27
  • 53
  • Really should use DateTime and related classes as this make the iteration requirement very simple. – Mike Brant Feb 28 '14 at 17:50
  • I am retracting my previous comment. Since OP is stuck on PHP 5.1.6 and doesn't have DateTime and associated classes, this sort of approach will likely be what he has to work with. – Mike Brant Feb 28 '14 at 18:43
  • if my date is in 'm.d.Y' format, does it not work? I keep getting 12.31.1969 as the final date. $date = date('m.d.Y'); $date = date('m.d.Y', strtotime($date .' -1 day')); – user2799603 Feb 28 '14 at 19:29