php 在php中获取今天和昨天的时间戳

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

Get timestamp of today and yesterday in php

phptimestamp

提问by hd.

How can I get the timestamp of 12 o'clock of today, yesterday and the day before yesterday by using strtotime() function in php?

如何在php中使用strtotime()函数获取今天、昨天和前天12点的时间戳?

12 o'clock is a variable and would be changed by user.

12 点钟是一个变量,可由用户更改。

回答by deceze

$hour = 12;

$today              = strtotime($hour . ':00:00');
$yesterday          = strtotime('-1 day', $today);
$dayBeforeYesterday = strtotime('-1 day', $yesterday);

回答by Emil H

strtotime supports a number of interesting modifiersthat can be used:

strtotime 支持许多可以使用的有趣的修饰符

$hour = 12;

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

echo date("Y-m-d H:i:s\n", $today);
echo date("Y-m-d H:i:s\n", $yesterday);
echo date("Y-m-d H:i:s\n", $dayBeforeYesterday);

It works as predicted:

它按预期工作:

2011-01-24 12:00:00
2011-01-23 12:00:00
2011-01-22 12:00:00

回答by enobrev

OO Equivalent

OO 等价物

$iHour = 12;

$oToday = new DateTime();
$oToday->setTime($iHour, 0);

$oYesterday = clone $oToday;
$oYesterday->modify('-1 day');

$oDayBefore = clone $oYesterday;
$oDayBefore->modify('-1 day');

$iToday     = $oToday->getTimestamp();
$iYesterday = $oYesterday->getTimestamp();
$iDayBefore = $oDayBefore->getTimestamp();

echo "Today: $iToday\n";
echo "Yesterday: $iYesterday\n";
echo "Day Before: $iDayBefore\n";

回答by zzapper

to get start of day yesterday

昨天开始一天

$oDate = new DateTime(); 
$oDate->modify('-1 day'); 
echo $oDate->format('Y-m-d 00:00:00');

result

结果

2014-11-05 00:00:00

回答by Nisam

You can easily find out any date using DateTimeobject, It is so flexible

您可以使用DateTime对象轻松找到任何日期,它是如此灵活

$yesterday = new DateTime('yesterday');
echo $yesterday->format('Y-m-d');

$firstModayOfApril = new DateTime('first monday of april');
echo $firstModayOfApril->format('Y-m-d');

$nextMonday = new DateTime('next monday');
echo $nextMonday->format('Y-m-d');

回答by Jean-Luc Barat

you can also use new DateTime("now")for today new DateTime("1 day ago")for yesterday or all can be parse by strtotimephp function.

您也可以将new DateTime("now")今天new DateTime("1 day ago")用于昨天,或者都可以通过strtotimephp 函数进行解析。

Then format as you want.

然后根据需要格式化。

回答by OZZIE

All the answers here are too long and bloated, everyone loves one-lines ;)

这里所有的答案都太长而且臃肿,每个人都喜欢单行;)

$yesterday = Date('Y-m-d', strtotime('-1 day'));

(Or if you are American you can randomize the date unit order to m/d/y (or whatever you use) and use Cups, galloons, feet and horses as units...)

(或者,如果您是美国人,您可以将日期单位顺序随机化为 m/d/y(或您使用的任何东西),并使用杯、加仑、英尺和马作为单位......)

回答by tolykot

$timeStamp = time();
// $timeStamp = time() - 86400;

if (date('d.m.Y', $timeStamp) == date('d.m.Y')) {    
  echo 'Today'; 
} elseif (date('d.m.Y', $time) == date('d.m.Y', strtotime('-1 day'))) {     
  echo 'Yesterday';
}

回答by COil

As of PHP 7 you can write something like this:

由于PHP 7,你可以写类似这样

$today = new \DateTime();
$yesterday = (clone $today)->modify('-1 day');
$dayBefore = (clone $yesterday)->modify('-1 day');
// Then call ->format('Y-m-d 00:00:00'); on each objects