php 删除时间戳的时间部分

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

Remove time part of a timestamp

phpdatetimetimestamp

提问by David Weng

How can I remove time part of a timestamp?

如何删除时间戳的时间部分?

So for example turn 1310571061to 1310565600which is the plain date timestamp.

因此,例如转13105710611310565600这是明摆着的日期戳。

回答by Brad

strtotime(date("Y-m-d", 1310571061));

That should do it for you.

那应该为你做。

回答by Brad

In case you wanted a mathematical solution:

如果你想要一个数学解决方案:

$time=1310571061;
echo floor($time/86400)*86400;

There are 86,400 seconds in 24 hours (which is the length of a typicalday, but not all... see DST). Since our timestamps are in UTC, this shouldn't be a problem. You can divide that by the number of seconds in a day, drop the remainder, and multiply back out.

24 小时内有 86,400 秒(这是典型一天的长度,但不是全部……参见夏令时)。由于我们的时间戳采用 UTC,因此这应该不是问题。您可以将其除以一天中的秒数,减去余数,然后再乘以返回。

回答by xtempore

Using a DateTime object can be helpful, and also can make the code more readable...

使用 DateTime 对象可能会有所帮助,并且还可以使代码更具可读性......

$dt = new DateTime();
$dt->setTimestamp(1310571061);
echo $dt->format('Y-m-d, H:i:s') . "\r\n";
$dt->setTime(0, 0, 0);
echo $dt->format('Y-m-d, H:i:s');

Result...

结果...

2011-07-14, 03:31:01

2011-07-14, 00:00:00

2011-07-14, 03:31:01

2011-07-14, 00:00:00

The choice of whether to use raw timestamps or DateTime objects will depend a lot on the implementation needs, but generally speaking DateTime objects will go a long way towards reducing confusion and errors that can crop up especially around Timezone issues and Daylight Saving Time issues.

选择是使用原始时间戳还是 DateTime 对象将在很大程度上取决于实现需求,但一般来说 DateTime 对象将大大有助于减少可能出现的混淆和错误,尤其是在时区问题和夏令时问题方面。

回答by Shef

Try:

尝试:

<?php
$ts = '1310571061';

echo strtotime(date('Y-m-d 00:00:00', $ts));
?>

回答by Rohit Srivastava

$pubdate='2003-02-19T00:00:00.000-05:00';

   $da = strtotime($pubdate);
   echo $dat = date('Y-m-d', $da);

The answer is like :"2003-02-19"

答案就像:“2003-02-19”

Thank You

谢谢你

回答by Elo Kitty

This is what I usually do:

这是我通常的做法:

your_timestamp = pd.to_datetime(your_timestamp.strftime(format="%x"))

The function strftimewill convert your_timestampto a string without the time component. Then the function pd.to_datetimewill convert it back to a Timestampwithout the time component.

该函数strftime将转换your_timestamp为没有时间分量的字符串。然后该函数pd.to_datetime会将其转换回Timestamp没有时间分量的 a 。

回答by Nicola Peluchetti

You could do:

你可以这样做:

$date = strotime(date("y/m/d", $timestamp));