time() Function Error

Problems with the Windows version of XAMPP, questions, comments, and anything related.

time() Function Error

Postby Ag2000 » 15. December 2023 22:55

It is an error with a function or my understanding ;)
When I use the time() function to capture the time, sleep(5) seconds, and capture the time again, the difference between between the two times is 1 hour and 5 seconds.

Code: Select all
<?php
$start_time = time();
$format = "%T";
$stime = strftime($format, $start_time);
print "Start Time: $stime<br>";
// do something that takes time
print "<br>doing something<br><br>";
sleep(5);
$stop_time = time();
$stime = strftime($format, $stop_time);
print "Stop Time: $stime<br>";
$dif_time = $stop_time - $start_time;
$strtime = strftime("%T", $dif_time);
print "Difference: $strtime <br>";
?>

results in:
Start Time: 22:42:46

doing something

Stop Time: 22:42:51
Difference: 01:00:05
Say what you will about Sisyphus. He always has work.
Ag2000
 
Posts: 14
Joined: 26. November 2023 02:09
XAMPP version: 8.2.12
Operating System: windows 11

Re: time() Function Error

Postby Ag2000 » 16. December 2023 22:33

Forgot, Windows 11 and XAMPP 8.2.12. Which I see can be implied ------------------------------------>
Say what you will about Sisyphus. He always has work.
Ag2000
 
Posts: 14
Joined: 26. November 2023 02:09
XAMPP version: 8.2.12
Operating System: windows 11

Re: time() Function Error

Postby Nobbie » 17. December 2023 00:11

Its not a function error, its your misunderstanding.

The time() returns the number of seconds which have passed since 1. January 1970. If you build the difference between two time() calls, it gives you the number of seconds between the two calls. You may simply print the difference. Instead you pass that difference to a function, which calculates the date and time from the number of seconds, which have passed since 1. January 1970 (probably starting at 1 o clock). You pass 5 seconds to that function. Thats plain nonsense.

The difference of two dates is not a date, got it? But its that what you are calculating.
Nobbie
 
Posts: 13176
Joined: 09. March 2008 13:04

Re: time() Function Error

Postby Ag2000 » 17. December 2023 03:47

Nobbie wrote:Its not a function error, its your misunderstanding.

The expected result. I was mindlessly trying to avoid the code to format 2-3 hrs of seconds to hrs/minutes/seconds.
While reading to improve understanding I see strftime() is deprecated and I should be using date().
To do the formatting I wanted I also see that using
Code: Select all
date_default_timezone_set('Etc/GMT');
does what I want without several divides by 60. Setting the time zone results in
Difference: 00:00:05

The actual time of day is a don't care. Thanks for the head slap.
Say what you will about Sisyphus. He always has work.
Ag2000
 
Posts: 14
Joined: 26. November 2023 02:09
XAMPP version: 8.2.12
Operating System: windows 11

Re: time() Function Error

Postby Nobbie » 17. December 2023 19:23

Ag2000 wrote:without several divides by 60


But thats the way to go.

Code: Select all
<?php

        $diff = 1235;  // Insert any test value here

        $seconds = (int) $diff;

        $minutes = (int) ($seconds/60);
        $seconds %= 60;

        $hours  = (int) ($minutes/60);
        $minutes %= 60;

        printf("%02d:%02d:%02d", $hours, $minutes, $seconds);
?>
Nobbie
 
Posts: 13176
Joined: 09. March 2008 13:04

Re: time() Function Error

Postby Ag2000 » 17. December 2023 20:38

Or think about it this way:
By subtracting the start_time from the stop_time the difference is: 1) the elapsed time and/or 2) a number, Unix timestamps, that represents the number of seconds after the Epoch (January 1 1970 00:00:00 GMT). Setting the default_timezone to GMT I get the desired formatted results with one line of code and avoid the adjustments for timezone.

There is more than one way to separate a cat from its skin.
Say what you will about Sisyphus. He always has work.
Ag2000
 
Posts: 14
Joined: 26. November 2023 02:09
XAMPP version: 8.2.12
Operating System: windows 11

Re: time() Function Error

Postby Nobbie » 17. December 2023 21:36

Its still nonsense. What will you do if you publish your script on a Webhosting Service with a different local setting? And also, what is if the number of seconds exceed one day? And so on - its simply wrong, even if it works in certain conditions.

As you can see in my little script, its so easy to do it properly.
Nobbie
 
Posts: 13176
Joined: 09. March 2008 13:04


Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 147 guests