Round datetime to last hour

我的梦境 提交于 2019-12-23 06:50:13

问题


I tried to look for this but I could not find good example of this what im trying to do.

I got datetime values in MySQL database that has to be rounded down when that value is on use.

Example, all these values:

2013-04-20 07:14:42
2013-04-20 07:19:51
2013-04-20 07:37:26
2013-04-20 07:46:28
2013-04-20 07:59:44

Should be rounded down to:

2013-04-20 07:00:00

And

2013-04-20 16:25:34

should be:

2013-04-20 16:00:00 etc...

PHP code that gets date value:

$d = strtotime($row["date"]);

So, how its possible to round down datetime value?


回答1:


Try this,

$date = "2013-04-20 16:25:34"; 
echo date("Y-m-d H:00:00",strtotime($date));

CodePad Demo.




回答2:


If you are using PHP's Carbon DateTime library (which you really should - https://github.com/briannesbitt/Carbon )

You can achieve it easily using

Carbon::now()->minute(0)->second(0);




回答3:


Since you already have a Unix timestamp $d, most efficient way is to use only arithmetic instead of date functions - especially if you're going to loop through a result set.

$hourTimestamp = $d - ($d % 3600);

Modulo operator gives you the remainder which you subtract from the timestamp to get hour timestamp.




回答4:


In that case a simple substr could do:

echo substr($date, 0, 13) . ":00:00";



回答5:


strtotime() gives you a Unix timestamp which is the number of seconds since 1970-01-01 00:00:00.

What if just divided by 3600 seconds (seconds equivalent to 1 hour) and ignore the remainders (the minutes and seconds you do want)?

$d = strtotime($row["date"]);
$rounded_d = intval($d / 3600);
$formatted_rounded_d = date('Y-m-d H:i:s', $rounded_d)



回答6:


you can use date and strtotime function function to achieve this, simply already change your minutes and second accordling

    $date = '2013-04-20 07:14:42';
    $newdate = date('Y-m-d H:00:00', strtotime($date));

    echo $newdate;

this will output

2013-04-20 07:00:00



回答7:


This writes the date to a string by outputting directly 00:00 as minutes and seconds instead of writing i:s:

$date = date("Y-m-d H:00:00", $d);

Or do you need it as unix timestamp? Then cut the minutes and the seconds off (always the last 5 bytes) and replace them by 00:00.

$d = strtotime(substr($row["date"], 0, -5)."00:00"));



回答8:


For those who follow (much later!): Carbon has an easy way to do this

$date = (new Carbon($row['date']))->minute(0)->second(0)->getTimestamp();



回答9:


I would use DateTime's setTime() method in a function like this.

<?php

/**
 * @param \DateTime $dateTime
 *
 * @return \DateTime
 */
function roundDownToHour(\DateTime $dateTime)
{
    $dt = $dateTime; // clone

    return $dt->setTime(
        $dateTime->format("H"),
        0,
        0
    );
}

$testDate = date("Y-m-d H:i:s", mktime(9, 59, 59, 1, 30, 2019));
$roundedDownToHour = roundDownToHour(new \DateTime($testDate));

var_dump($testDate); //=> 2019-01-30 9:59:59
var_dump($roundedDownToHour->format("Y-m-d H:i:s")); //=> 2019-01-30 09:00:00

Which results to the following.

// the results
string(19) "2019-01-30 09:59:59"
string(19) "2019-01-30 09:00:00"


来源:https://stackoverflow.com/questions/16118565/round-datetime-to-last-hour

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!