Dhanapal
Dhanapal

Reputation: 380

Time addition not working in PHP

I have assigned the variable as

$tot_time=strtotime("h:i:s",  time());

then i add the time using a loop with:

$tot_time=  strtotime($tot_time)+strtotime($result_row->time);

then, i print the total time after the loop using:

echo date("h:i:s",$tot_time);

i get the last values from the loop.

Upvotes: 0

Views: 68

Answers (1)

Marc B
Marc B

Reputation: 360572

You're badly abusing strtotime(). WHy not just leave the time values as integers as they already are?

$tot_time += strtotime($result_row->time);

$tot_time is ALREADY an integer, so there is literally no point in running it through strtotime(). And you can't really use this method for adding together time intervals. PHP's date system works on timestamps, which are seconds-since-1970.

If your $tot_time naturally exceeds 24 hours, you're going to get a "tomorrow" time, e.g. You yon't get "25 hours", you'll get "1 day and 1 hour". You'll only print out "1hour", and now your time interval is very very wrong.

Upvotes: 3

Related Questions