By td-dev


2011-10-04 16:45:24 8 Comments

I'm trying to take a date object that's coming out of my Drupal CMS, subtract one day and print out both dates. Here's what I have

$date_raw = $messagenode->field_message_date[0]['value'];

print($date_raw);

//this gives me the following string: 2011-04-24T00:00:00

$date_object = date_create($date_raw);

$next_date_object = date_modify($date_object,'-1 day');

print('First Date ' . date_format($date_object,'Y-m-d'));

//this gives me the correctly formatted string '2011-04-24'

print('Next Date ' . date_format($next_date_object,'Y-m-d'));

//this gives me nothing. The output here is always blank

So I'm not understanding why the original date object is coming out fine, but then I'm trying to create an additional date object and modify it by subtracting one day and it seems like I can't do that. The output always comes out blank.

10 comments

@Militaru 2019-07-25 17:17:03

Simple like that:

date("Y-m-d", strtotime("-1 day"));

date("Y-m-d", strtotime("-1 months"))

@JIYAUL MUSTAPHA 2018-12-15 07:12:20

How to add 1 year to a date and then subtract 1 day from it in asp.net c#

Convert.ToDateTime(txtDate.Value.Trim()).AddYears(1).AddDays(-1);

@atheaos 2019-04-02 17:01:09

This is not PHP.

@cuperto 2019-03-22 08:57:00

Answear taken from Php manual strtotime function comments :

echo date( "Y-m-d", strtotime( "2009-01-31 -1 day"));

Or

$date = "2009-01-31";
echo date( "Y-m-d", strtotime( $date . "-1 day"));

@Armfoot 2016-03-22 12:37:00

A one-liner option is:

echo date_create('2011-04-24')->modify('-1 days')->format('Y-m-d');

Running it on Online PHP Editor.


mktime alternative

If you prefer to avoid using string methods, or going into calculations, or even creating additional variables, mktime supports subtraction and negative values in the following way:

// Today's date
echo date('Y-m-d'); // 2016-03-22

// Yesterday's date
echo date('Y-m-d', mktime(0, 0, 0, date("m"), date("d")-1, date("Y"))); // 2016-03-21

// 42 days ago
echo date('Y-m-d', mktime(0, 0, 0, date("m"), date("d")-42, date("Y"))); // 2016-02-09

//Using a previous date object
$date_object = new DateTime('2011-04-24');
echo date('Y-m-d',
  mktime(0, 0, 0,
     $date_object->format("m"),
     $date_object->format("d")-1,
     $date_object->format("Y")
    )
); // 2011-04-23

Online PHP Editor

@user3413723 2015-06-11 04:58:42

How about this: convert it to a unix timestamp first, subtract 60*60*24 (exactly one day in seconds), and then grab the date from that.

$newDate = strtotime($date_raw) - 60*60*24;
echo date('Y-m-d',$newDate);

Note: as apokryfos has pointed out, this would technically be thrown off by daylight savings time changes where there would be a day with either 25 or 23 hours

@Yevgeniy Afanasyev 2015-08-18 06:52:46

Good one, only I made it like this : $newDate =date_create($date_raw)->getTimestamp() - 60*60*24;

@apokryfos 2016-08-16 14:33:18

This assumes that all days are 24 hours long which is not true in those 2 days when a minority of the world moves their clocks by +/- 1 hour to provide an illusion of more or less sunlight. And then there's leap seconds as well.

@user3413723 2016-08-16 18:59:55

that's a great catch @apokryfos. I wouldn't use this for a super scientific calculation, but in the vast majority of cases it should still be just fine.

@aberdat 2017-05-18 23:09:17

$date = new DateTime("2017-05-18"); // For today/now, don't pass an arg.
$date->modify("-1 day");
echo $date->format("Y-m-d H:i:s");

Using DateTime has significantly reduced the amount of headaches endured whilst manipulating dates.

@Armfoot 2018-02-02 22:42:08

date_create is the procedural method to invoke the DateTime constructor, which allows you to reduce your 3 lines into 1 without using new. I.e. echo date_create('2017-05-18')->modify('-1 day')->format('Y-m-d H:i:s');

@Cosmitar 2013-08-06 13:57:01

Object oriented version

$dateObject = new DateTime( $date_raw );
print('Next Date ' . $dateObject->sub( new DateInterval('P1D') )->format('Y-m-d');

@Burki 2015-08-18 08:23:06

your answer adds one day instead of substracting it. Other than that it would be my preferred solution.

@MasterCassim 2011-10-04 16:51:47

You can try:

print('Next Date ' . date('Y-m-d', strtotime('-1 day', strtotime($date_raw))));

@sHaDeoNeR 2015-01-20 16:09:07

You can get the $date_raw same format with $date_raw = date("r");

@Tip-Sy 2016-01-27 10:24:57

Be careful! If you handle dates beyond 2038 it will not work because of the integer size limit in PHP (at least on a 32bit platform). In that case, the safest solution is to use DateTime (see here).

@iThink 2011-10-04 16:56:41

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

@Clive 2011-10-04 16:52:06

Not sure why your current code isn't working but if you don't specifically need a date object this will work:

$first_date = strtotime($date_raw);
$second_date = strtotime('-1 day', $first_date);

print 'First Date ' . date('Y-m-d', $first_date);
print 'Next Date ' . date('Y-m-d', $second_date);

@td-dev 2011-10-04 17:17:55

Thanks Clive! That one worked for me...yeah, I didn't really need date objects, I just needed to print the formatted dates.

Related Questions

Sponsored Content

20 Answered Questions

[SOLVED] Reference — What does this symbol mean in PHP?

26 Answered Questions

[SOLVED] How do I get PHP errors to display?

37 Answered Questions

[SOLVED] PHP and Enumerations

  • 2008-10-31 18:51:14
  • Henrik Paul
  • 495342 View
  • 1157 Score
  • 37 Answer
  • Tags:   php enumeration

33 Answered Questions

[SOLVED] startsWith() and endsWith() functions in PHP

  • 2009-05-07 12:14:27
  • Click Upvote
  • 853531 View
  • 1498 Score
  • 33 Answer
  • Tags:   php string

38 Answered Questions

[SOLVED] Deleting an element from an array in PHP

  • 2008-12-15 20:28:55
  • Ben
  • 2693400 View
  • 2560 Score
  • 38 Answer
  • Tags:   php arrays unset

28 Answered Questions

[SOLVED] How can I prevent SQL injection in PHP?

36 Answered Questions

[SOLVED] Reference - What does this error mean in PHP?

13 Answered Questions

[SOLVED] Why shouldn't I use mysql_* functions in PHP?

  • 2012-10-12 13:18:39
  • Madara's Ghost
  • 223597 View
  • 2522 Score
  • 13 Answer
  • Tags:   php mysql

30 Answered Questions

[SOLVED] How do you parse and process HTML/XML in PHP?

7 Answered Questions

[SOLVED] How does PHP 'foreach' actually work?

Sponsored Content