Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.9k views
in Technique[技术] by (71.8m points)

datetime - add two or more time strings in php

I have an array with times (string) e.g "2:23", "3:2:22" etc.

$times = array("2:33", "4:2:22", "3:22") //loner

I want to find the total sum of all array.

Is there a way that I could add times like "2:33" and "3:33" ("i:s")

thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You might want to look at the PHP date/time functions - one option would be to use something like strtotime():

$midnight = strtotime("0:00");

// ssm = seconds since midnight
$ssm1 = strtotime("2:33") - $midnight;
$ssm2 = strtotime("3:33") - $midnight;

// This gives you the total seconds since midnight resulting from the sum of the two
$totalseconds = $ssm1 + $ssm2; // will be 21960 (6 hours and 6 minutes worth of seconds)

// If you want an output in a time format again, this will format the output in
// 24-hour time:
$formattedTime = date("G:i", $midnight + totalseconds);

// $formattedTime winds up as "6:06"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...