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?
$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.
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. Most popular library for processing DateTimes in PHP is Carbon.
Here you would simply do:
$yesterday = Carbon::now()->subDay(); DateTime->sub(), whcih is the correct way to subtract from a DateTime. Upvote - this ought to have been the accepted answer.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 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;
strtotimeexpects a timestamp as the second value. (In your example, you could just omit it completely)mis whereiusually sits in the time portion? Every one of the answers copy pasted this oversight. Ha.i,mare months