43

I have the following code:

 $now = date("Y-m-d H:m:s"); $date = date("Y-m-d H:m:s", strtotime('-24 hours', $now)); 

However, now it gives me this error:

A non well formed numeric value encountered in... 

why is this?

5
  • 4
    See the manual - strtotime expects a timestamp as the second value. (In your example, you could just omit it completely) Commented Jul 18, 2013 at 8:16
  • 2
    You might want to consideer day time saving borders: do you really want a swith from 10 AM to 11 AM (or 9 AM?). Commented Jul 18, 2013 at 8:19
  • 1
    ...did anyone else catch that m is where i usually sits in the time portion? Every one of the answers copy pasted this oversight. Ha. Commented Mar 22, 2017 at 1:43
  • @mickmackusa You are absolutely right. Minutes are formatted with i, m are months Commented May 24, 2017 at 10:12
  • Why have't you accepted an answer to this very basic and very well recived question? Doing so would help others. Btw, I think that @Joel has given the best answer. Commented Dec 30, 2019 at 13:46

11 Answers 11

66
$date = (new \DateTime())->modify('-24 hours'); 

or

$date = (new \DateTime())->modify('-1 day'); 

(The latter takes into account this comment as it is a valid point.)

Should work fine for you here. See http://PHP.net/datetime

$date will be an instance of DateTime, a real DateTime object.

Sign up to request clarification or add additional context in comments.

3 Comments

this answer modifies the original DateTime object in-place.
Yes, that's the way \DateTime works. If that isn't good for you, use \DateTimeImmutable instead.
ah thank you! didn't know DateTimeImmutable was a thing.
54

strtotime() expects a unix timestamp (which is number seconds since Jan 01 1970)

$date = date("Y-m-d H:i:s", strtotime('-24 hours', time())); ////time() is default so you do not need to specify. 

i would suggest using the datetime library though, since it's a more object oriented approach.

$date = new DateTime(); //date & time of right now. (Like time()) $date->sub(new DateInterval('P1D')); //subtract period of 1 day 

The advantage of this is that you can reuse the DateInterval:

$date = new DateTime(); //date & time of right now. (Like time()) $oneDayPeriod = new DateInterval('P1D'); //period of 1 day $date->sub($oneDayPeriod); $date->sub($oneDayPeriod); //2 days are subtracted. $date2 = new DateTime(); $date2->sub($oneDayPeriod); //can use the same period, multiple times. 

Carbon (update 2020)

Most popular library for processing DateTimes in PHP is Carbon.

Here you would simply do:

$yesterday = Carbon::now()->subDay(); 

1 Comment

This is the only answer given which uses DateTime->sub(), whcih is the correct way to subtract from a DateTime. Upvote - this ought to have been the accepted answer.
11

you can do this in many ways...

echo date('Y-m-d H:i:s',strtotime('-24 hours')); // "i" for minutes with leading zeros 

OR

echo date('Y-m-d H:i:s',strtotime('last day')); // 24 hours (1 day) 

Output

2013-07-17 10:07:29 

1 Comment

The big difference does your application expect a string or an object. If string then the string way is fine but if object then you have to use DateTime. While the output might look the same the data type is not.
5

Simplest way to sub or add time,

<?php **#Subtract 24 hours** $dtSub = new DateTime('- 24 hours'); var_dump($dtSub->format('Y-m-d H:m:s')); **#Add 24 hours** $dtAdd = new DateTime('24 hours'); var_dump($dtAdd->format('Y-m-d H:m:s'));die; ?> 

Comments

3

This may be helpful for you:

//calculate like this $date = date("Y-m-d H:m:s", (time()-(60*60*24))); //check the date echo $date; 

1 Comment

Downvoted for unreliability: sandbox.onlinephpfunctions.com/code/…
2

this should work, too

$date = date("Y-m-d H:m:s", strtotime('-24 hours')); 

Comments

2
$now = date("Y-m-d H:i:s"); $date = date("Y-m-d H:i:s", strtotime('-24 hours', strtotime($now))); 

Add "strtotime" before $now, and Y-m-d H:m:s replace with Y-m-d H:i:s

Comments

1

You can simply use time() to get the current timestamp.

$date = date("Y-m-d H:m:s", strtotime('-24 hours', time())); 

Comments

1

In same code use strtotime() its working.

$now = date("Y-m-d H:i:s"); $date = date("Y-m-d H:i:s", strtotime('-2 hours', strtotime($now))); 

Comments

1

Try this :

$now = time(); $date = date("Y-m-d H:m:s", strtotime('-24 hours', $now)); 

Comments

0

all you have to do is to alter your code to be

$now = strtotime(date("Y-m-d H:m:s")); $date = date("Y-m-d H:m:s", strtotime('-24 hours', $now)); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.