We now have a youtube channel. Subscribe!

PHP | gettimeofday() Function

PHP gettimeofday() Function


Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP gettimeofday() Function.

The PHP gettimeofday() function returns the current time of the day. By default, this PHP function returns the current time as an array. If you pass the boolean value true as an argument, this function returns this time as floating point number.

Syntax

Following below is the syntax to use this function -

gettimeofday($return_float)


Parameter Details

Sr.NoParameter & Description
1

return_float($Optional)

This is a boolean value which is used to specify whether the time should be a floating point value or not. If this value is true, this function returns time as floating point value.


Return Value

This PHP function returns the current time. By default this value is going to be an array with keys: sec, usec, minuteswest, and the dsttime. If you set return_float value to true, then the time will be returned as as floating point value.

PHP Version

This function was first introduced as part of core PHP version 4 and, it works with all the later versions.

Example1

Following example illustrates the usage of the PHP gettimeofday() function -

<?php
   $time = gettimeofday();    
   print_r($time);   
?>

Output

When the above code is executed, it will produce the following result -

Array
(
    [sec] => 1589298247
    [usec] => 881165
    [minuteswest] => 0
    [dsttime] => 0
)

Example2

The following example prints the current time as floating point -

<?php
   $time = gettimeofday(true);    
   print_r($time); 
?>

Output

When the above code is executed, it will produce the following result -

1605840411.0555

Example3

You can extract individual values of time as shown below -

<?php
   $time = gettimeofday();
   echo "sec: $time[sec]\n";
   echo "usec: $time[usec]\n";
   echo "minuteswest: $time[minuteswest]\n";
   echo "dsttime: $time[dsttime]"; 
?>

Output

When the above code is executed, it will produce the following result -

sec: 1589301022
usec: 843115
minuteswest: 0
dsttime: 0

Example4

Try the following example -

<?php
   print_r(gettimeofday());   
   echo gettimeofday(true);
?>

Output

When the above code is executed, it will produce the following result -

Array
(
    [sec] => 1589261767
    [usec] => 31653
    [minuteswest] => 0
    [dsttime] => 0
)
1589261767.032


Alright guys! This is where we are going to be rounding up for this tutorial post. In our next tutorial, we are going to be discussing about the PHP gmdate() Function.

Do feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2023 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain