/    Sign up×
Community /Pin to ProfileBookmark

Time Calculation

Hi,

If something starts at 12:00 and ends at 5:00 thats 5 hours.

I am trying to calculate the duration:
When I add this to the page that I am viewing, <? echo($user_data[‘duration’]); ?> nothing happens.

This is what I have so far

[code=php]
//Calculate Duration
if(isset($user_data[‘startTime’]) && $user_data[‘endTime’] != “00:00”)
$start = strtotime($user_data[‘startTime’]);
$end = strtotime($user_data[‘endTime’]);
$timeSeconds = $end – $start;
$user_data[‘duration’] = round($timeSeconds / 3600, 1);
[/code]

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@bfsogFeb 26.2006 — For a start, you have no { and } round your code that dhould run if the if passes true.
Copy linkTweet thisAlerts:
@comptech520authorFeb 26.2006 — the { and } are in the file I am working with, there are just a bunch of other things there so I left it out, but it is there.
Copy linkTweet thisAlerts:
@SheldonFeb 26.2006 — [code=php]
if(isset($user_data['startTime']) && ($user_data['endTime'] != "00:00"))
$start = strtotime($user_data['startTime']);
$end = strtotime($user_data['endTime']);
$timeSeconds = $end - $start;
$user_data['duration'] = round($timeSeconds / 3600, 1);
[/code]

you were missing the brackets around the second string

*($user_data['endTime'] != "00:00"))*

You dont need the braces if you dont have an else

but lets add them and try some debugging
[code=php]
if(isset($user_data['startTime']) && ($user_data['endTime'] != "00:00")){
$start = strtotime($user_data['startTime']);
$end = strtotime($user_data['endTime']);
$timeSeconds = $end - $start;
$user_data['duration'] = round($timeSeconds / 3600, 1);
echo("Success"); /* It is working*/
}else{
echo("start time-".$user_data['startTime'].",<br />end time-=".$user_data['endTime']);
}
/*the else will echo
"start time-,
end time -"
*/ or if only one is set it will echo it, letting you know which is wrong
[/code]
Copy linkTweet thisAlerts:
@comptech520authorFeb 26.2006 — Thanks, but what I was looking for is to calculate the duration

so if the event started at 7:00 AM and ended at 2:00 PM that would be 7 hours (the duration)

Thanks
Copy linkTweet thisAlerts:
@comptech520authorMar 02.2006 — Any luck on this on calculating the duration?

Thanks
Copy linkTweet thisAlerts:
@SheldonMar 02.2006 — well, you coult try work in 24 hour time and minus the higer time from the lower time.

php.net/
Copy linkTweet thisAlerts:
@NogDogMar 02.2006 — Use strtotime() and/or mktime() to convert the time strings into system time (which is in seconds), Subtract the lesser value from the greater, then divide by 3600 (number of seconds in an hour) to get the difference in hours.
Copy linkTweet thisAlerts:
@SpectreReturnsMar 03.2006 — You dont need the braces if you dont have an else[/QUOTE]
I thought you needed braces if you're using more than one statement to branch. Seems the only logical way to be parsed, unless the parser pays attention to indenting... I'm going to go test it.

Tested, and I'm right. Try this baby:
[code=php]if (false)
echo "foo<br>";
echo "bar";[/code]

Output is 'bar'.

Also, when using the no braces syntax, you can use an else:

[code=php]if (false) echo "baz";
else echo "quux";[/code]


As for this:
the { and } are in the file I am working with, there are just a bunch of other things there so I left it out, but it is there.[/QUOTE]
Bad idea. If you need help, post things exactly as you have them, so any problems can be recreated.
×

Success!

Help @comptech520 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...