Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP date yesterday [duplicate]

Tags:

date

php

People also ask

How can I get yesterday date in PHP?

Using time() to Get Yesterday's Date in PHP The time() function returns the current timestamp. If we subtract its value, then we get the timestamp of the same time yesterday.

How to modify date in PHP?

PHP date_modify() Function $date=date_create("2013-05-01"); date_modify($date,"+15 days"); echo date_format($date,"Y-m-d");

What is Strtotime PHP?

The strtotime() function parses an English textual datetime into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT). Note: If the year is specified in a two-digit format, values between 0-69 are mapped to 2000-2069 and values between 70-100 are mapped to 1970-2000.

How can I get tomorrow date in PHP?

$newDate = date('Y-m-d', strtotime('tomorrow')); echo $newDate; ?>


date() itself is only for formatting, but it accepts a second parameter.

date("F j, Y", time() - 60 * 60 * 24);

To keep it simple I just subtract 24 hours from the unix timestamp.

A modern oop-approach is using DateTime

$date = new DateTime();
$date->sub(new DateInterval('P1D'));
echo $date->format('F j, Y') . "\n";

Or in your case (more readable/obvious)

$date = new DateTime();
$date->add(DateInterval::createFromDateString('yesterday'));
echo $date->format('F j, Y') . "\n";

(Because DateInterval is negative here, we must add() it here)

See also: DateTime::sub() and DateInterval


strtotime(), as in date("F j, Y", strtotime("yesterday"));


How easy :)

date("F j, Y", strtotime( '-1 days' ) );

Example:

echo date("Y-m-j H:i:s", strtotime( '-1 days' ) ); // 2018-07-18 07:02:43

Output:

2018-07-17 07:02:43