php 如何使用php获取昨天的日期?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16009152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:21:47  来源:igfitidea点击:

how to get date of yesterday using php?

phpdate

提问by unique_programmer

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

我想在 php 中使用特定日期格式获取昨天的日期,这是格式:

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

Is it possible?

是否可以?

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

如果应分别更改月份和年份,请考虑月份和年份。

回答by Fabio

there you go

你去吧

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

this will work also if month change

如果月份更改,这也将起作用

回答by Gaurang P

Step 1

第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().

我们需要在函数 date() 中设置格式数据:函数 date() 返回一个根据给定格式字符串格式化的字符串,使用给定的整数时间戳或当前时间(如果没有给定时间戳)。换句话说,timestamp 是可选的,默认为 time() 的值。

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

result: March 30, 2010

结果:2010 年 3 月 30 日

Step 2

第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 函数 mktime():函数 mktime() 返回对应于给定参数的 Unix 时间戳。此时间戳是一个长整数,包含 Unix 纪元(1970 年 1 月 1 日 00:00:00 GMT)和指定时间之间的秒数。参数可以按从右到左的顺序省略;任何因此省略的参数都将根据本地日期和时间设置为当前值。

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

result: 1269820800

结果:1269820800

Step 3

第 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

结果:2010 年 3 月 29 日

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

结果:20:00:00

or 7 days ago:

或 7 天前:

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

result: 2010-03-23

结果:2010-03-23

回答by alwaysLearn

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');

        ?>           

回答by NullPoiиteя

you can do this by

你可以这样做

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

or by

或由

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

回答by liyakat

try this

尝试这个

<?php
$yesterday = date(“d.m.Y”, time()-86400);
echo $yesterday;

回答by Dhara Talaviya

Yesterday Date in PHP:

PHP 中的昨天日期:

echo date("Y-m-d", strtotime("yesterday")); 

回答by Fabien Snauwaert

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

如果您在 PHP 应用程序中定义时区(如您所愿),您可以这样做:

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.)

(您可能需要定义一个常量或环境变量来存储您的默认时区。)

回答by ns16

You can also do this using Carbonlibrary:

您也可以使用Carbon库执行此操作:

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'