Hi. I am going nuts.
This code should output 0:01:10 but instead is returning 18:01:10
Any ideas? (tested in my xampp and in my server)
echo strftime("%H:%M:%S", 70);
70 is the number of seconds isn’t?
Best regards
Hi. I am going nuts.
This code should output 0:01:10 but instead is returning 18:01:10
Any ideas? (tested in my xampp and in my server)
echo strftime("%H:%M:%S", 70);
70 is the number of seconds isn’t?
Best regards
70 is the number of seconds past the 1st of January, 1970 00:00:00 UTC; so the code is behaving properly.
If you want 70 seconds into the future, use strtotime('+70 seconds')
as the second parameter.
Thanks Anthony, this clarify some things.
Actually the code I am using is:
strftime("%H:%M:%S", ($end_time - $initial_time)
I don’t get how to subtract the origin time. Using:
($end_time-$initial_time)-mktime(0,0,0,0,0,0)
gave me 12:01:10
This difference is the one I need and is variable.
Do I explain myself clear?
bit of a weird one really (this cant be right…)
echo date('H:i:s', strtotime('midnight +70 seconds'));
// gives
// 00:01:10
Better read up on the formatting options, not quite as you might imagine…
Youre probably best telling us where these time values are coming from and where they are going to, is it to a JS countdowner by any chance?
Here you have a test example:
<?php
$end_time = mktime(8,57,22);
$initial_time = mktime(8,56,12);
echo strftime("%H:%M:%S", $initial_time) . "<br />";
echo strftime("%H:%M:%S", $end_time) . "<br />";
echo strftime("%H:%M:%S", ($end_time - $initial_time)) . "<br />";
?>
Gives:
08:56:12
08:57:22
01:01:10
$tgt = $end_time - $initial_time;
$mins = $tgt / 60;
$secs = $tgt % 60;
$hrs = $tgt / 3600;
$format = "%02d:%02d:%02d";
printf($format, $hrs, $mins, $secs);
//00:01:10
You did not answer my question, and you simply have misunderstood how strftime() works, but still, the above should get you what you asked for.
Yes, your are correct. I didn’t. Where are coming from or where are going to is not relevant for this exercise. The sample I gave shows the idea.
Yes, I don’t get how strftime works that’s why I asked and still don’t get it. The manual says: strftime — Format a local time/date according to locale settings
And as far I understand, 70 second (this example) are 0:01:10
But you also didn’t answer the strftime question, you gave another solution (Thanks for that)
My question is why strftim(“%H:%M:%S”, 70) (or the difference between to valid times) has an hour more 1:01:10 instead 0:01:10
Oh, its just an exercise … I thought you were trying to solve a real-world problem and just had to get working code out of the door.
Still, you haven’t answered the question.
Is not an exercise. I just put the relevant code for you as you requested.
The answer you gave is a solution as I said, but as you are proficient in strftime, are you kind enough to share that knowledge and tell me why the result is not 0:01:10?
I’m no date expert, but I’m going to guess timezones/offsets are in play here.
UK - Europe/London
<?php
if(!setlocale(LC_TIME, 'uk')){
echo 'Cannot set locale';
exit;
}
if(!date_default_timezone_set('Europe/London')){
echo 'Cannot set timezone';
exit;
}
function display_time($time, $format = '%H:%M:%S'){
return strftime($format, $time) . PHP_EOL;
}
$from = mktime(15, 30, 00);
$to = mktime(16, 45, 00);
echo 'From: ', display_time($from);
echo 'To: ', display_time($to);
echo 'Difference: ', display_time($to - $from);
/*
From: 15:30:00
To: 16:45:00
Difference: 02:15:00
*/
<?php
if(!setlocale(LC_TIME, 'uk')){
echo 'Cannot set locale';
exit;
}
if(!date_default_timezone_set('Europe/London')){
echo 'Cannot set timezone';
exit;
}
function display_time($time, $format = '%H:%M:%S'){
return strftime($format, $time) . PHP_EOL;
}
$from = new DateTime('15:30:00');
$to = new DateTime('16:45:00');
$diff = $to->diff($from);
echo 'From: ', display_time($from->getTimestamp());
echo 'To: ', display_time($to->getTimestamp());
echo 'Difference: ', $diff->format('%H'), ':', $diff->format('%I'), ':', $diff->format('%S');
/*
From: 15:30:00
To: 16:45:00
Difference: 01:15:00
*/
USA - America/New_York
<?php
if(!setlocale(LC_TIME, 'usa')){
echo 'Cannot set locale';
exit;
}
if(!date_default_timezone_set('America/New_York')){
echo 'Cannot set timezone';
exit;
}
function display_time($time, $format = '%H:%M:%S'){
return strftime($format, $time) . PHP_EOL;
}
$from = mktime(15, 30, 00);
$to = mktime(16, 45, 00);
echo 'From: ', display_time($from);
echo 'To: ', display_time($to);
echo 'Difference: ', display_time($to - $from);
/*
From: 15:30:00
To: 16:45:00
Difference: 20:15:00
*/
<?php
if(!setlocale(LC_TIME, 'usa')){
echo 'Cannot set locale';
exit;
}
if(!date_default_timezone_set('America/New_York')){
echo 'Cannot set timezone';
exit;
}
function display_time($time, $format = '%H:%M:%S'){
return strftime($format, $time) . PHP_EOL;
}
$from = new DateTime('15:30:00');
$to = new DateTime('16:45:00');
$diff = $to->diff($from);
echo 'From: ', display_time($from->getTimestamp());
echo 'To: ', display_time($to->getTimestamp());
echo 'Difference: ', $diff->format('%H'), ':', $diff->format('%I'), ':', $diff->format('%S');
/*
From: 15:30:00
To: 16:45:00
Difference: 01:15:00
*/
Can you see how both the OOP style methods report the correct difference? Yet, when we change the timezone strftime changes too?
I’ll leave it to you to investigate further.
I am a date expert, and am telling you that timezones/offsets are in play here. (:
echo "My default timezone is: " . date_default_timezone_get();
P.S. There is also the option of using gmstrftime()
which does not care about the script’s timezone setting.
Priceless, cheers Peter.
It’s always nice to receive confirmation of my, often silly, ramblings.
Thanks you Salathe and Anthony for your help.
I am looking at this right now.
Have a great day.