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?
Try this,
$date = "2013-04-20 16:25:34";
echo date("Y-m-d H:00:00",strtotime($date));
CodePad Demo.
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);
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.
In that case a simple substr could do:
echo substr($date, 0, 13) . ":00:00";
Refering to #Dayson response, this is a better way to round datetime to last hour with Carbon
$dt = Carbon::create(2012, 1, 31, 15, 32, 45);
echo $dt->startOfHour(); // 2012-01-31 15:00:00
check the Modifier section in Carbon Doc
For those who follow (much later!): Carbon has an easy way to do this
$date = (new Carbon($row['date']))->minute(0)->second(0)->getTimestamp();
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
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"));
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)
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"
It's important you use a built-in PHP function for rounding times to take into account the date as well as the time. For example 2020-10-09 23:37:35 needs to become 2020-10-10 00:00:00 when rounding up to nearest hour.
Round time to nearest hour:
$time = '2020-10-09 23:37:35';
$time = date("Y-m-d H:i:s", round(strtotime($time) / 3600) * 3600); // 2020-10-10 00:00:00
$time = '2020-10-09 23:15:35';
$time = date("Y-m-d H:i:s", round(strtotime($time) / 3600) * 3600); // 2020-10-09 23:00:00
Round time to nearest 20 minute increment:
$time = '2020-10-09 23:15:35';
$time = date("Y-m-d H:i:s", ceil(strtotime($time) / (60*20))*(60*20)); // 2020-10-09 23:20:00
$time = '2020-10-09 23:41:35';
$time = date("Y-m-d H:i:s", ceil(strtotime($time) / (60*20))*(60*20)); // 2020-10-10 00:00:00
If you need to round down to nearest 20 minute increment, change ceil to floor e.g
$time = date("Y-m-d H:i:s", floor(strtotime($time) / (60*20))*(60*20)); // 2020-10-09 23:40:00
If you need to round time to another minute increment you can simply do:
$time = date("Y-m-d H:i:s", ceil(strtotime($time) / (60*15))*(60*15)); // 2020-10-09 23:45:00
Related
I have this 13 digit timestamp 1443852054000 that i want to convert to date and time but dont succeed. I have tried this codes:
echo date('Y-m-d h:i:s',$item->timestamp);
doesnt work for me and also this
$unix_time = date('Ymdhis', strtotime($datetime ));
and this :
$item = strtotime($txn_row['appoint_date']);
<?php echo date("Y-m-d H:i:s", $time); ?>
what should i use?
This timestamp is in milliseconds, not in seconds. Divide it by 1000 and use date function:
echo date('Y-m-d h:i:s', $item->timestamp / 1000);
// e.g
echo date('Y-m-d h:i:s',1443852054000/1000);
// shows 2015-10-03 02:00:54
A 13 digit timestamp is used in JavaScript to represent time in milliseconds. In PHP 10 a digit timestamp is used to represent time in seconds. So divide by 1000 and round off to get 10 digits.
$timestamp = 1443852054000;
echo date('Y-m-d h:i:s', floor($timestamp / 1000));
You can achieve this with DateTime::createFromFormat.
Because you've a timestamp with 13 digits, you'll have to divide it by 1000, in order to use it with DateTime, i.e.:
$ts = 1443852054000 / 1000; // we're basically removing the last 3 zeros
$date = DateTime::createFromFormat("U", $ts)->format("Y-m-d h:i:s");
echo $date;
//2015-10-03 06:00:54
DEMO
http://sandbox.onlinephpfunctions.com/code/d0d01718e0fc02574b401e798aaa201137658acb
You may want to set the default timezone to avoid any warnings
date_default_timezone_set('Europe/Lisbon');
NOTE:
More about php date and time at php the right way
In my project time shows as
"2017-01-01 12:00:00+03:00".
How to make it as
"2017-01-01 15:00:00"
?
You can use like this
echo date("Y-m-d H:i:s A",strtotime('2017-01-01 12:00:00+03:00'));
But you will get 09:00:00 AM not 15:00:00 AM because your time is +3 ahead
https://eval.in/822865
Your time 2017-01-01 12:00:00+03:00 means that it is hour 12:00:00 in the timezone of +3 hours. You don't want to do anything just to reformat your time to different display:
(new DateTime($yourDateInStringOrUnix))->format('Y-m-d H:i:s');
and you will get
`2017-01-01 12:00:00` //This is local time in timezone of +3 hour
You want to add offset to your current time. Separate datetime with offset and recalculate it.
<?php
date_default_timezone_set('UTC');
$date = '2017-01-01 12:00:00+03:00';
$datetime = substr($date, 0, 19); // Get date time string
$offset = substr($date, 19); // Extract Offset
list($hours, $minutes) = explode(':', $offset);
$seconds = $hours * 60 * 60 + $minutes * 60; // Convert offset to seconds
// Recalculate date time by adding offset
echo date('Y-m-d H:i:s', strtotime($datetime) + $seconds);
?>
Output
2017-01-01 15:00:00
Hope this helps!
You can make it like this.
If this is coming from your data you can do it like this:
$my_date = "2017-01-01 12:00:00+03:00";
$converted_date = date('Y-m-d H:i:s', strtotime($my_date));
echo $converted_date;
But if you are getting the current date and time only you can make it this way;
$my_date = date('Y-m-d H:i:s');
echo $my_date;
I have this 13 digit timestamp 1443852054000 that i want to convert to date and time but dont succeed. I have tried this codes:
echo date('Y-m-d h:i:s',$item->timestamp);
doesnt work for me and also this
$unix_time = date('Ymdhis', strtotime($datetime ));
and this :
$item = strtotime($txn_row['appoint_date']);
<?php echo date("Y-m-d H:i:s", $time); ?>
what should i use?
This timestamp is in milliseconds, not in seconds. Divide it by 1000 and use date function:
echo date('Y-m-d h:i:s', $item->timestamp / 1000);
// e.g
echo date('Y-m-d h:i:s',1443852054000/1000);
// shows 2015-10-03 02:00:54
A 13 digit timestamp is used in JavaScript to represent time in milliseconds. In PHP 10 a digit timestamp is used to represent time in seconds. So divide by 1000 and round off to get 10 digits.
$timestamp = 1443852054000;
echo date('Y-m-d h:i:s', floor($timestamp / 1000));
You can achieve this with DateTime::createFromFormat.
Because you've a timestamp with 13 digits, you'll have to divide it by 1000, in order to use it with DateTime, i.e.:
$ts = 1443852054000 / 1000; // we're basically removing the last 3 zeros
$date = DateTime::createFromFormat("U", $ts)->format("Y-m-d h:i:s");
echo $date;
//2015-10-03 06:00:54
DEMO
http://sandbox.onlinephpfunctions.com/code/d0d01718e0fc02574b401e798aaa201137658acb
You may want to set the default timezone to avoid any warnings
date_default_timezone_set('Europe/Lisbon');
NOTE:
More about php date and time at php the right way
I have the data 2013-02-04 03:20:00
How do I arrive into this 07:20 using php.
Same is through with this 2013-02-04 08:00:00 to this 12:00
I have this data labeled timestamp_diff which is 14400
Any idea? Thank You
I think the answer was base on the timestamp_diff not just adding 4 or any values to it.
Is there any datetime() function(s) to get the equivalent of 14400 to 4
<?php
$add4hour = time() + (4*60*60);
$newTime = date("d m Y H:i:s",$add4hour);
echo $newTime;
?>
Edit because of your requirements
<?php
$add4hour = time() + (4*60*60); // 4 hour adding
$d1 = date("d-m-Y H:i:s"); // date 1 assume that hour 03:20:00
$d2 = date("d-m-Y H:i:s",$add4hour); // date 2, Assume that hour 07:20:00 by adding 4hour
$d1_timestamp = strtotime($d1); // first date's timestamp
$d2_timestamp = strtotime($d2); // second date's timestamp
$time_diff = $d2_timestamp - $d1_timestamp; //difference
echo $time_diff; // this will give you 14400
?>
$date = date("Y-m-d H:i:s");
$addhr= 4;//Replace you value to be added here
$format="H:i";
echo $date."<br>";
echo date($format, strtotime("$date + $addhr hours"));
You can use \DateTime class and add any interval with DateTime::add. Also you can use procedure style.
Say i have a variable like $speed = 5.5, what formula would I use to convert that into minutes, so in this case it would be 5 and a half minutes.
I need it to work in this:
date("Y-m-d H:i:s", strtotime("$now - $speed mins"));
Other examples, 2.25 would convert to 2 mins 15 secs, 7:75 to 7 mins 45 secs, etc
Anyone have any ideas? Never been a maths buff.
Just do it with second.
date('Y-m-d H:i:s', strtotime(sprintf('- %d second', $speed * 60)));
If you want more precision, then
date('Y-m-d H:i:s', strtotime(sprintf('- %d second', round($speed * 60))));
You could also use PHP's own DateInterval class (requires PHP 5.3) http://www.php.net/manual/en/dateinterval.createfromdatestring.php
With sample:
$interval = DateInterval::createFromDateString('5.5 minutes');
echo $interval->format('%Y-%m-%d %H:%i:%s')
Could also a Unix Timestamp for dates and 3600 = 1 hour.
For example, the current time would be: $timestamp = gmmktime();
So if you wanted to add ".5" (30 minutes) to the current time, you would say $timestamp + 1800. ".25" would be $timestamp + 900.
$minutes = floor($speed);
$seconds = ($speed - $minutes) * 60;