删除时间戳的时间部分

发布于 2024-11-20 00:25:59 字数 95 浏览 2 评论 0原文

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

例如,将 1310571061 转换为 1310565600,这是纯日期时间戳。

How can I remove time part of a timestamp?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

獨角戲 2024-11-27 00:25:59
strtotime(date("Y-m-d", 1310571061));

那应该适合你。

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

That should do it for you.

绿光 2024-11-27 00:25:59

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

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

24 小时有 86,400 秒(这是典型一天的长度,但并非全部......请参阅 DST)。由于我们的时间戳采用 UTC,因此这应该不是问题。您可以将其除以一天中的秒数,去掉余数,然后再相乘。

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 typical day, 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.

后来的我们 2024-11-27 00:25:59

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

结果...

2011-07-14, 03:31:01

2011-07-14, 00:00:00

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

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

$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

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.

陪你到最终 2024-11-27 00:25:59

尝试:

<?php
$ts = '1310571061';

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

Try:

<?php
$ts = '1310571061';

echo strtotime(date('Y-m-d 00:00:00', $ts));
?>
空‖城人不在 2024-11-27 00:25:59
$pubdate='2003-02-19T00:00:00.000-05:00';

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

答案如下:“2003-02-19”

谢谢

$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"

Thank You

残月升风 2024-11-27 00:25:59

你可以这样做:

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

You could do:

$date = strotime(date("y/m/d", $timestamp));   
◇流星雨 2024-11-27 00:25:59

这就是我通常做的事情:

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

函数 strftime 会将 your_timestamp 转换为不带时间部分的字符串。然后函数pd.to_datetime会将其转换回不带时间分量的Timestamp

This is what I usually do:

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

The function strftime will convert your_timestamp to a string without the time component. Then the function pd.to_datetime will convert it back to a Timestamp without the time component.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文