I have noticed that regardless of a given script's execution time, every date() call will return the same timestamp regardless of where the function is called within the script. It looks like it just returns the time at which the script first started executing.
For logging purposes, it would be extremely useful to be able to get incremental timestamps from within a script. Is this possible? Is there a way to do this that is relatively lightweight?
Edit: Would the example for the microtime() function suggests it might do this. Can anyone confirm?
Update: microtime() does work, but I cannot format it with the date() function because date() only accepts timestamps as integers (so no microseconds). How can I get a properly formatted date from the value returned by microtime() ?
First of all, date() is used to format a timestamp - it's just the default behaviour that it'll use the current timestamp, when date() is called without second parameter. The timestamp used will be the timestamp the moment the function is called - calling date('Y-m-d') is the same as calling date('Y-m-d', time()) where time() will give you the
[...] the current time measured in the
number of seconds since the Unix Epoch
(January 1 1970 00:00:00 GMT).
You only get the same timestamp every time you call date() because your script is too fast and runs within one second resulting in no timestamp-change.
To address your second problem of formatting the microtime() return value, you can do the following (untested)
function formatTime($microtime, $format)
{
list($timestamp, $fraction) = explode('.', $microtime);
return date($format, (int)$timestamp) . '.' . $fraction;
}
The value given to $microtime should be a float, which you get when you pass true as a parameter to microtime()
I ran this code on my machine:
<?php
$time_start = time();
sleep(2);
$time_end = time();
print 'Start: ' . date("m/d/Y # g:i:sA", $time_start) . '<br>';
print 'End: ' . date("m/d/Y # g:i:sA", $time_end);
?>
And it output:
Start: 10/23/2008 # 3:12:23PM
End: 10/23/2008 # 3:12:25PM
Leading me to believe time() does not just return the time when execution started.
http://us.php.net/microtime gives me different times within the same script.
You can use the Pear Benchmarking package for getting timing and profiling information.
Related
echo strtotime('2017-05-03 16:16:01');
echo "<br>";
echo time();
I ran this query at 2017-05-03 16:28:01 so output of time() should be higher and strtotime('2017-05-03 16:16:01'); should be lower but I am not getting the output.
above code prints
1493828161
1493809172 //
But I expected it to print if I run the query at 2017-05-03 16:02:01 same timestamp
1493809172
1493809172
But it prints with big difference.
time() return dynamic current timestamp which will change every moment.
Where as your passed date is static date, which will convert it into its timestamp() value and will show it.
Conclusion : time() is that which can not be stopped, it run as time pass by whereas timestamp with given date time, it will always show static value timestamp of given date time.
strtotime('2017-05-01 16:02:01')
Convert provided time into string to time
time ()
That convert to Returns the current time as a Unix timestamp
i'm wondering if there's a way to get any datetime in microseconds.
I was looking forward microtime(), but it just returns the date in the moment.
Anyone knows if is this possible?
I have my date given like: Y-m-d H:i:s.u.
I was thinking about something like (Y-1970)*31556926 + m*151200+d*86400+h*3600+m*60+s.u
but I don't know if that's why i'm a beginner on programming, but i can't think in a way to separate each: Y,m... to do the math.
Would appreciate any help/suggestions.
You can do this with DateTime:
$date = DateTime::createFromFormat('Y-m-d H:i:s.u', '2000-01-01 13:12:12.129817');
echo $date->format('U.u');
//prints: 946728732.129817
What I would do is simply split apart your input format like this:
Y-m-d H:i:s and u
You should be able to do this by exploding on . in your input formatted date string. Then just calculate the UNIX timestamp on the whole second portion. Finally just add your fraction second portion back to the timestamp (via either string concatenation of arithmetic depending on whether you want string or float as result).
No it's not possible. Timestamps are integers internally, describing the seconds from 01.01.1970 GMT. Regardless if you are using a 32 or 64 bit system, the microseconds from 1970 would lead to an overflow as there has much too many of them gone.
You updated the question.. Yes, it is possible to display the current time in microsends using microtime():
$microtime = microtime();
// will return something like:
// 0.40823900 1381181037
// where the first are the micros and the last a regular time stamp
list($micros, $timestamp) = explode(' ', $microtime);
echo date('Y-m-d H:i:s', intval($timestamp)) . '+'
. (floatval($micros) * 1000000) . 'msec';
I'd like my user to be able to input values like:
4 hours
23 minutes
etc.
strtotime works great for converting these values into seconds, but it adds them to the current time. Is there a way of getting it to return the quantity of time entered in total, rather than from now? Or do I need to do something like this:
$time = strtotime($value) - time();
And just for arguments sake, what would happen if the value of time changes between strtotime evaluating it and time evaluating it?
$time = strtotime($value, 0);
http://php.net/strtotime
$time = strtotime($value) - time();
And just for arguments sake, what would happen if the value of time changes between strtotime evaluating it and time evaluating it?
Then you will miss some seconds (probably 1), because strtotime($value) is evaluated first and if the time goes by the result of time() will be bigger then expected (1 second probably ;))
You can make sure you're always working with the same time using this pattern:
$start = time();
$time = strtotime($value, $start) - $start;
Or even easier, skip the subtraction by setting the second argument to 0:
$time = strtotime($time, 0);
Following javascript code gives me output like : 1314066350368
new Date().getTime();
I would like to generate similar timestamp in PHP in order to get server's timestamp.
kindly suggest me how to do it in PHP so my PHP code will generate exactly similar timestamps.
P.S. I do know about time() and microtime() functions in PHP. But i am looking forward to get similar outputs as i have mentioned above.
Try this:
<?php
echo microtime(true)*1000;
?>
Did you try int time ( void ), Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
the following php results time stamp that is similar to javascript 'new Date().getTime()'
function microtime_float()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec) * 100;
}
The output of the following program can be seen here: http://codepad.org/egNGJBUL
<?php
/* Checking if time() is really timezone independent */
date_default_timezone_set('UTC');
echo time();
echo "\n";
date_default_timezone_set('Australia/Queensland');
echo time();
echo "\n";
/* Using date() function passing timestamp parameter */
date_default_timezone_set('UTC');
echo date('Y-m-d H:i:s',time());
echo "\n";
date_default_timezone_set('Australia/Queensland');
echo date('Y-m-d H:i:s',time());
echo "\n";
/* Using date() function without passing timestamp parameter */
date_default_timezone_set('UTC');
echo date('Y-m-d H:i:s');
echo "\n";
date_default_timezone_set('Australia/Queensland');
echo date('Y-m-d H:i:s');
echo "\n";
From line 1-2 of the output, we can see time() returns a value which is really timezone independent.
In line 3-4, it's strange that date() function ignores the timestamp parameter and still display the date time according to the timezone set.
Why is it like this?
Not really sure what you are expecting to see, but yes, looks very normal to me.
A timestamp is a integer counted from a certain point in time (usually the UNIX EPOCH). While the display of this value is timezone independent, it is no more or less so that say, the value of a properly formatted date, notated with a timezone, is timezone independent...
example, all of the following statements are both true (logically)
1297799809 == 1297799809
2011-02-15 19:56:49 (UTC) == 2011-02-16 05:56:49 (Austria/Queensland)
All time is 'timezone independant'. Timezones only affect the way we display a particular moment in time.
date() functions second parameter, if not specified, is time() value.
date() Returns a string formatted according to the given format string using the given integer timestamp or the current time if no timestamp is given. In other words, timestamp is optional and defaults to the value of time().
from date()'s manual
So actually nothing is being ingnored.
The date function returns the date of a timestamp calculated for the current timezone, as others have said, if no timestamp is passed to it, then the current time is used for the timestamp, so passing time() is the same as not passing anything at all.
However, doing something like $time = time();sleep 5;echo date($format,$time); will get you a date 5 seconds in the past.
It's meant to display the date formatted for current timezone so you can have a universal method of keeping time that's constant across computers/servers and be easily parsable, and yet be able to display the date in any timezone desired.
The UTC timezone is actually the time that the timestamp is calculated to, more precisely, the number of seconds since 00:00 Jan 1, 1970 UTC, then it adds or subtracts 3600 (60*60) seconds from/to the timestamp per hour offset from UTC time to get the time in the currently set timezone.