134

I want to get the yesterday date using specific date format in php this is the format:

$today = date("d.m.Y"); //15.04.2013

Is it possible?

Take consideration of month and years if they should be changed in respective.

Leandro Bardelli
  • 10,561
  • 15
  • 79
  • 116
unique_programmer
  • 1,561
  • 3
  • 11
  • 14

12 Answers12

359

there you go

date('d.m.Y',strtotime("-1 days"));

this will work also if month change

Fabio
  • 23,183
  • 12
  • 55
  • 64
  • A small nit-pick: Your function gets into trouble near midnight if that time spans DST-change. Explicitly going over noon helps there. – Deduplicator Nov 23 '14 at 16:08
  • 3
    **Wrong**. 3 things about DST: 1 it depends on server time, if server has correct time zone it won't affect my function. 2 not the whole world is affect by DST, there are place that doesn't use it. 3 most of the places use to change time during the first hours of the day (like 2 or 3 am) so it won't affect a changing day. – Fabio Nov 23 '14 at 22:16
  • 1
    1. So that function knows and takes heed that a day can be other than 24h? 2. Even a broken clock is right twice daily. 3. The exact hour which is stolen/duplicated is not that important for the calculation. – Deduplicator Nov 24 '14 at 00:33
  • 2
    **no sense** 1 a day is not other than 24 untill DST change the time and the server changes time according 2 of course it is but that's not the point 3 this is very important instead, your idea could be right if the time change at midnight, not in other hours – Fabio Nov 24 '14 at 01:13
26

Yesterday Date in PHP:

echo date("Y-m-d", strtotime("yesterday")); 
4b0
  • 21,981
  • 30
  • 95
  • 142
Dhara Talaviya
  • 572
  • 7
  • 10
14

try this

        $tz    = new DateTimeZone('Your Time Zone');
        $date  = new DateTime($today,$tz);
        $interval = new DateInterval('P1D');
        $date->sub($interval); 

        echo $date->format('d.m.y');

        ?>           
alwaysLearn
  • 6,882
  • 7
  • 39
  • 67
13

If you define the timezone in your PHP app (as you should), which you can do this way:

date_default_timezone_set('Europe/Paris');

Then it's as simple as:

$yesterday = new DateTime('yesterday'); // will use our default timezone, Paris
echo $yesterday->format('Y-m-d'); // or whatever format you want

(You may want to define a constant or environment variable to store your default timezone.)

Fabien Snauwaert
  • 4,995
  • 5
  • 52
  • 70
11

you can do this by

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

or by

date("F j, Y", strtotime("yesterday"));
NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
8

Step 1

We need set format data in function date(): Function date() returns a string formatted according to the givenformat string using the given integer timestamp or the current time ifno timestamp is given. In other words, timestampis optional anddefaults to the value of time().

<?php
echo date("F j, Y");
?>

result: March 30, 2010

Step 2

For "yesterday" date use php function mktime(): Function mktime() returns the Unix timestamp corresponding to thearguments given. This timestamp is a long integer containing the numberof seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and thetime specified. Arguments may be left out in order from right to left; any argumentsthus omitted will be set to the current value according to the localdate and time.

<?php
echo mktime(0, 0, 0, date("m"), date("d")-1, date("Y"));
?>

result: 1269820800

Step 3

Now merge all and look at this:

<?php
$yesterday = date("Y-m-d", mktime(0, 0, 0, date("m") , date("d")-1,date("Y")));
echo $yesterday;
?>

result: March 29, 2010

Operating similarly, it is possible to receive time hour back.

<?php
$yesterday = date("H:i:s",mktime(date("H"), 0, 0, date("m"),date("d"), date("Y")));
echo $yesterday;
?>

result: 20:00:00

or 7 days ago:

<?php
$week = date("Y-m-d",mktime(0, 0, 0, date("m"), date("d")-7,date("Y")));
echo $week;
?>

result: 2010-03-23

GIPSSTAR
  • 2,050
  • 1
  • 25
  • 20
4

Another OOP method for DateTime with setting the exact hour:

$yesterday = new DateTime("yesterday 09:00:59", new DateTimeZone('Europe/London'));
echo $yesterday->format('Y-m-d H:i:s') . "\n";
leviathan
  • 43
  • 1
  • 7
2

try this

<?php
$yesterday = date(“d.m.Y”, time()-86400);
echo $yesterday;
liyakat
  • 11,825
  • 2
  • 40
  • 46
2

Most of the answers are in procedural/mixed style. Here is the solution in pure OOP style:

$today = new DateTime();
$yesterday = $today->sub(new DateInterval("P1D"));
echo $yesterday->format("d.m.Y");

It will take care of the daylight saving and timezone issues as well. Simple solution.

Raptor
  • 53,206
  • 45
  • 230
  • 366
0

You can also do this using Carbon library:

Carbon::yesterday()->format('d.m.Y');         // '26.03.2019'

In other formats:

Carbon::yesterday()->toDateString();          // '2019-03-26'
Carbon::yesterday()->toDateTimeString();      // '2019-03-26 00:00:00'

Carbon::yesterday()->toFormattedDateString(); // 'Mar 26, 2019'
Carbon::yesterday()->toDayDateTimeString();   // 'Tue, Mar 26, 2019 12:00 AM'
ns16
  • 1,322
  • 2
  • 17
  • 26
  • It's a good idea if you have so many date/time in your project to work with. makes it easy to develop. but if you have only few case to convert, you better to do that with pure PHP, because of system performances. – Zargaripour Oct 11 '21 at 17:03
0

We can use the Fancy method. Like this for yesterday. By today

$date = new DateTime; // example 2021-04-02
date_sub($date, date_interval_create_from_date_string('1 days')); 
$date = date_format($date, 'Y-m-d'); // Output will be 2021-04-01

Or we can find yesterday by a specific date like this.

$date = date_create('2021-04-06');
date_sub($date, date_interval_create_from_date_string('5 days'));
$date = date_format($date, 'Y-m-d'); // The output will be 2021-04-01
0

Create a function & use 'Carbon API'

function getYesterday()
{
    return \Carbon\Carbon::yesterday()->format('Y-m-d');
}

Call the function

getYesterday();