Hướng dẫn php microtime to minutes

I have a session which contains microtime(true) value. like below:

Yii::app()->user->setState('portal_logged_time', microtime(true));

Now i want to add 15 minutes in above session, but it should remain in microtime.

$starttime = Yii::app()->user->getState('portal_logged_time');
// Now i want to add microtime of 15 minutes into $starttime
$endtime = // i want to do something like this ($starttime*15*60); 

I have tried this but it is not working $starttime*15*60.

So how to achieve that?

After that i am trying to fetch remaining minutes and seconds:

$duration = $endtime-$starttime;

$hours = (int)($duration/60/60);
$minutes = (int)($duration/60)-$hours*60;
$seconds = (int)$duration-$hours*60*60-$minutes*60;

asked Sep 13, 2016 at 8:57

Hướng dẫn php microtime to minutes

DS9DS9

2,8934 gold badges49 silver badges100 bronze badges

6

Values provided by microtime() are in seconds. So you should simply do:

$endtime = $starttime + 15*60;

answered Sep 13, 2016 at 9:27

Hướng dẫn php microtime to minutes

Frosty ZFrosty Z

21.6k11 gold badges81 silver badges110 bronze badges

3

Note that the timestamp returned is "with microseconds", not "in microseconds". This is especially good to know if you pass 'true' as the parameter and then calculate the difference between two float values -- the result is already in seconds; it doesn't need to be divided by a million.

";

echo date('Y-m-d h i s', $temp);

$sum = round($temp) + 900;

echo "
"; echo date('Y-m-d h i s', $sum); ?>

Check here

answered Sep 13, 2016 at 9:43

HmmmHmmm

5623 silver badges11 bronze badges

like this:

echo date("H:i:s",$endtime-$starttime);

Manually :

$duration = $endtime-$starttime;
$hours = (int)($duration/60/60);
$minutes = (int)($duration/60)-$hours*60;
$seconds = (int)$duration-$hours*60*60-$minutes*60;

answered Sep 13, 2016 at 9:02

Hướng dẫn php microtime to minutes

Hiren MakwanaHiren Makwana

1,8562 gold badges12 silver badges27 bronze badges

1

Try this:

$starttime = round(microtime(true) * 1000);

$endtime = $starttime + 15*60*1000;

answered Sep 13, 2016 at 9:12

2

From the PHP.net article on date() which is similar to gmdate(), except that the time is returned in GMT:

Nội dung chính

  • What is the PHP Microtime function?
  • How to get the current Unix timestamp in PHP using Microtime?
  • How to get the current time in seconds in PHP?
  • What does Microtime () return in Python?
  • How to convert microtime to seconds in PHP?
  • How to get microtime in PHP?
  • What is microtime?

Since this function only accepts integer timestamps the u format character is only useful when using the date_format() function with user based timestamps created with date_create().

Use something like this instead:

list($usec, $sec) = explode(' ', microtime()); //split the microtime on space
                                               //with two tokens $usec and $sec

$usec = str_replace("0.", ".", $usec);     //remove the leading '0.' from usec

print date('H:i:s', $sec) . $usec;       //appends the decimal portion of seconds

Which prints: 00:00:03.1745569706

If you want you can use round() to round the $usec var even more.

If you use microtime(true) use this instead:

list($sec, $usec) = explode('.', microtime(true)); //split the microtime on .

What is the PHP Microtime function?

The PHP microtime function is the one that returns the current Unix timestamp with microseconds. And interestingly, you can use the said function to get time in other units as well, such as seconds. So, here you’ll see how the PHP microtime works, how you can use the same for converting time units, and which other function returns time in seconds.

How to get the current Unix timestamp in PHP using Microtime?

PHP microtime () function returns the current Unix timestamp. By default this returns a string value in the form msec sec. If you pass the boolean value true as a parameter to this method, it returns the current time in seconds since the Unix epoch accurate to the nearest microsecond.

How to get the current time in seconds in PHP?

If you pass the boolean value true as a parameter to this method, it returns the current time in seconds since the Unix epoch accurate to the nearest microsecond. This function was first introduced in PHP Version 4 and, works with all the later versions. Following example demonstrates the usage of the microtime () function −

What does Microtime () return in Python?

If as_float is set to true, then microtime () returns a float, which represents the current time in seconds since the Unix epoch accurate to the nearest microsecond.

How to convert microtime to seconds in PHP?

PHP microtime() function returns the current Unix timestamp. By default this returns a string value in the form msec sec. If you pass the boolean value true as a parameter to this method, it returns the current time in seconds since the Unix epoch accurate to the nearest microsecond.

How to get microtime in PHP?

The microtime() function is an inbuilt function in PHP which is used to return the current Unix timestamp with microseconds. The $get_as_float is sent as a parameter to the microtime() function and it returns the string microsec sec by default.

What is microtime?

Definition of microtime : a very short interval of time (as 0.01 millionth of a second) microtime photography.