So I need to convert an unix timestamp which is in seconds to milliseconds.
This line seems to be not working
$unixtime = strtotime($timestamp_conv."+3 hour") * 1000;
I basically need a 13 digit timestamp.
Any ideas what I'm doing wrong?
Thanks
As per the comments, $timestamp_conv already is a (second-)timestamp, you want to convert to a (milliseconds-)timestamp. You, however, also try to add some offset (3 hours) to it.
With simple arithmetics this would look like this
// add the three hours: 3 hours of 60 minutes of 60 seconds each
$timestamp_conv += 3 * 60 * 60;
// convert to milliseconds base
$unixtime = $timestamp_conv * 1000;
You can use DateTime from php, it's more intuitive.
<?php
$t = new \DateTime();
$t->setTimestamp(1492498242);
$t->modify("+3 hours");
echo $t->getTimestamp()*1000;
Hope it helps!
Related
I have a string in the format of hh:mm:ss that I convert to an integer representing the total number of seconds. For example:
01:43:03
01 * 3600
43 * 60
03 * 1
The above example results in an integer value of 6183.
After performing some logic using this value I then need to convert the integer back to a strict format of hh:mm:ss.
Note that this isn't a time of day. It is an amount of time. Like, I spent 1 hour, 43 minutes and 3 seconds driving from one point to another.
What is the shortest way of coding this conversion so that 6183 seconds is formatted as 01:43:03 ?
Use this-
echo $time = date("h:i:s A T",'6183');
I think it will help.
You can simply use like this
<?php
$total_seconds = 160183;
$seconds = intval($total_seconds%60);
$total_minutes = intval($total_seconds/60);
$minutes = $total_minutes%60;
$hours = intval($total_minutes/60);
echo "$hours:$minutes:$seconds";
?>
Check live demo : http://sandbox.onlinephpfunctions.com/code/78772e1462879ce3a20548a3a780df5de4e16e2c
When I call strtotime("2016-05-06 15:00:00 +15.98 hours") I'd expect 2016-05-07 06:58:48 but instead I get 2016-05-10 02:00:00. What gives?
You can test here yourself:
Use strtotime: http://php.fnlist.com/date_time/strtotime
Convert output int to timestamp: http://www.epochconverter.com/
Floats aren't supported in date formatting in PHP
It seems that you can't do addition in strtotime that way. In addition, a bug for decimal point has been reported here
What you can do is add the time in 2 separate variable just like William's answer
Try this instead:
//60 * 60 * 15.98 = 57,528 seconds
$add = round(60 * 60 * 15.98);
$timestamp = strtotime("2016-05-06 15:00:00") + $add;
$dt = date("Y-m-d H:i:s", $timestamp);
echo $dt; //2016-05-07 06:58:48
This will calculate to 2016-05-07 06:58:48
As for why it incorrectly added the 15.98 hours is more complex. There has been a reported bug for this problem by PHP, though currently floats aren't supported in date formatting in PHP. Since you can not directly use floats in date formatting, you must substitute something like "1.5 years" with "18 months", or do arithmetic before and round it like this:
//60 * 60 * 15.98 = 57,528 seconds
$timeToAdd = round(60 * 60 * 15.98);
And then call strtotime() like in the above example
$date = strtotime("2016-05-06 15:00:00") + $timeToAdd;
I have stored date field at DB.
In PHP, i am getting that field and converted into date.
I want to compare that time with current time. If that difference is above 60 minutes. It will return some value.
I dont know how to write logic for that
$lastUpdatedField = $rows_fetch['lastUpdatedTime'];
$lastUpdatedDate = new DateTime($lastUpdatedField);
$nowDate = new DateTime(date('y-m-d h:m:s'));
I have old date&time is in $lastUpdatedDate variable, and current time is in $nowDate.
How to compare these two
$interval = $nowDate->diff($lastUpdatedDate);
echo $interval->h;
DateDiff: http://www.php.net/manual/en/datetime.diff.php
DateInterval: http://www.php.net/manual/en/class.dateinterval.php
Had The same problem earlier its actually quit simple
heres the piece where you declare your variables
$lastUpdateddate = new DateTime($lastUpdatedField);
$nowDate = new DateTime(date('y-m-d h:m:s'));
Then you have to convert them to second - format so that you can do math with them
To do that use strtotime
$Diff = strtotime($lastUpdatedDate) - strtotime($nowDate);
Then just check to see if the difference in time is more then 60 minutes,
So devide by 60 seconds to get minutes and by 60 to get hours
if ($diff/60/60 <= 1){
//do your thing here
{
First convert the current time and old time to one unit like Unix timestamp passing it through strtotime(). Then differentiate both the timestamp to get the difference between two times.
$difftime = strtotime(date('Y-m-d H:i:s')) - strtotime($rows_fetch['lastUpdatedTime']);
Then convert the difference to days as follows :
$days=$difftime/24*60*60;
Once you get the days you can get the minutes from it as below to compare to meet your need.
$timediff = $days * 24 * 60;
I have a variable $route->flighttime which echoes numbers in the following format...
0.5
1.2
1.45
How do I convert it to display HH:MM using echo?
Note: 0.5 is treated by the CMS as 50 minutes for some reason.
Based on OPs comments about the format of his initial time (where .5 is actually 50 minutes), a solution without any math
$time = floatval($route->flighttime);
echo number_format($time,2,':',null);
If the input is a floating point value of hours
First convert the floating point value to hours/minutes:
$hours = floor($route->flighttime);
$minutes = ($route->flighttime - $hours) * 60;
If the input is a hours/minutes pair tortured to fit into a float
You can either multiply by 100 instead of 60, or use this technique which might be more accurate:
list($hours, $minutes) = explode('.', sprintf('%.2F', $route->flighttime));
In any case
Then use printf (or some similar function such as sprintf) to format it properly:
printf("%02d:%02d", $hours, $minutes);
I'm assuming 0.5 means half of an hour. Use gmdate with format G:i and your time in seconds:
echo gmdate('G:i', round($x * 3600));
gmstrftime("%H:%M", $t * 60 * 60);
... where $t is float in hours. Notice use of gmstrftime instead of strftime, becouse you do not want to play with timezones.
If the current format is hour and minutes separated by a dot, you could use DateTime:
$time = DateTime::createFromFormat('G.i', '9.15');
echo $time->format('H:i');
However, in this format, 5 in the minutes section would be 5 minutes, not 50 minutes.
I am developing a quiz site and there is time for x min to answer the quiz. So when user clicks on start quiz link the starttime (current time at this instant) is recored in session. Also the endtime (start_time+ 30 min) is recorded in session and every time he submits a answer the current time is compared with the quiz end time. Only if the current time is less than end_time the answer should be accepted.
How can I get the currentdatetime?
How can I add x minutes to current this datetime?
How can I compare (<=) datetime ?
I think we should use date time. Is it right?
PHP measures time as seconds since Unix epoch (1st January 1970). This makes it really easy to work with, since everything just a single number.
To get the current time, use: time()
For basic maths like adding 30 minutes, just convert your interval into seconds and add:
time() + 30 * 60 // (30 * 60 ==> 30 minutes)
And since they're just numbers, just do regular old integer comparison:
$oldTime = $_SESSION['startTime'];
$now = time();
if ($now < $oldTime + 30 * 60) {
//expired
}
If you need to do more complicated things like finding the date of "next tuesday" or something, look at strtotime(), but you shouldn't need it in this case.
use php builtin functions to get time:
<?php
$currentTimeStamp = time(); // number of seconds since 1970, returns Integer value
$dateStringForASpecificSecond = date('Y-m-d H:i:s', $currentTimeStamp);
?>
for your application that needs to compare those times, using the timestamp is more appropriate.
<?php
$start = time();
$end = $start + (30 * 60); // 30 minutes
$_SESSION['end_time'] = $end;
?>
in the page where the quiz is submitted:
<?php
$now = time();
if ( $now <= $_SESSION['end_time'] ) {
// ok!
}
?>
Use the time() function to get a UNIX timestamp, which is really just a large integer.
The number returned by time() is the number of seconds since some date (like January 1, 1970), so to add $x minutes to it you do something like (time() + ($x*60)).
Since UNIX timestamps are just numbers, you can compare them with the usual comparison operators for numbers (< <= > >= ==)
time() will give you the current time in seconds since 1/1/1970 (an integer), which looks like it should be good.
To add x minutes, you'd just need to add x*60 to that, and you can compare it like any other two integers.
Source: http://us3.php.net/time
This is an old question but I wanted to provide an answer based on the PHP 5.2 DateTime class which I feel is much easier to use and much more versatile than any previous functions.
So how can i get the currentdatetime?
You can create a new DateTime object like this:
$currentTime = new DateTime();
But at this point, $currentTime is a datetime object and must be converted to a string in order to store it in a database or output it.
$currentTime = $currentTime->format('Y-m-d H:i:s');
echo $currentTime;
Outputs 2014-05-10 21:14:06
How can i add x minutes tocurrent this datetime?
You can add x minutes with the modify method:
$currentTime = new DateTime();
$addedMinutes = $currentTime->modify('+10 minutes');
echo $addedMinutes;
Outputs 2014-05-10 21:24:06
How can i comapare (<=) datetime ?
With the DateTime class, you can not only easily compare datetime objects, you can get the difference between them.
$currentTime = new DateTime('2014-05-10 21:14:06');
$addDays = $currentTime->modify('+10 days');
To compare
if ($currentTime >= $addDays) {
//do something//
}
$diffTime = new DateTime('2014-05-10 21:14:06');
$diff = $addDays->diff($diffTime);
$diff = $diff->format('There are %d days difference.');
echo $diff;
Outputs There are 10 days difference.