php 如何在PHP中获得前一天

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31921857/
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 22:43:26  来源:igfitidea点击:

how to get previous day in PHP

php

提问by Christofer Hansen

i now how to get the current date in PHP like so---

我现在如何像这样在 PHP 中获取当前日期---

echo date("Y/m/d")

But somehow i can not get how i can yet the previous day. I mean today is 2015-08-10, so how i can get yesterday date 2015-08-09.

但不知何故,我无法理解我前一天的情况。我的意思是今天是 2015-08-10,所以我怎么能得到昨天的日期2015-08-09

UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 DAY)

Is it the solution, but do not know how it can solve my problem.

它是解决方案,但不知道它如何解决我的问题。

Do anyone knows how to get previous day in the PHP.

有谁知道如何在 PHP 中获得前一天。

I have looked a lot but did not find any simple solution for this, any one knows any solution for this problem. Thanks in advanced.

我看了很多,但没有找到任何简单的解决方案,任何人都知道这个问题的任何解决方案。提前致谢。

回答by Hirdesh Vishwdewa

Use this-

用这个-

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

回答by Ankur Tiwari

Try this

尝试这个

date('Y-m-d', strtotime('-1 day', strtotime('2015-08-10')))

And you will get the previous day of 2015-08-10, To get the previous date from current date simple use this.

您将获得 2015-08-10 的前一天,要从当前日期获取前一天,只需使用它。

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

For more details follow the official site

更多详情请关注官

回答by piddl0r

You can modify a PHP DateTime object, it's fairly simple to add or subtract date intervals, this is the first example in the manual.

您可以修改 PHP DateTime 对象,添加或减去日期间隔相当简单,这是手册中的第一个示例。

<?php
    $date = new DateTime('2006-12-12');
    $date->modify('+1 day');
    echo $date->format('Y-m-d');
?>

date modify

日期修改

回答by Tharif

Get yesterday :

获取昨天:

$hour = 12;    
$today              = strtotime("$hour:00:00");
$yesterday          = strtotime("-1 day", $today);