Click here to Skip to main content
15,748,749 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hello!

I want to convert a float/double integer value to a time stamp in PHP.

PHP
0.555 = 1:19:12 PM
// in Microsoft excel we use =TimeValue(0.555) 
// it return value: 1:19:12 PM

Thanks for your interest!
Posted
Updated 9-Aug-11 2:59am
v2
Comments
CPallini 9-Aug-11 8:30am    
Excel's TimeValue does the opposite, according to the documentation:
http://office.microsoft.com/en-us/excel-help/timevalue-HP005209316.aspx?CTT=5&origin=HP005199659
CPallini 9-Aug-11 8:59am    
There's a not empty intersection of PHP and VBA functionalities, I guess.
Manfred Rudolf Bihy 9-Aug-11 9:07am    
I just realized OP had written Excel in his post :doh:
I think I need to check more closely next time :)
CPallini 9-Aug-11 9:09am    
I suppose I've accidentally deleted your reply. Sorry for that.
Manfred Rudolf Bihy 9-Aug-11 10:36am    
Nope that was me! As I realized my blunder I deleted the post, but since you already replied we can still see that it once was there. (Only the originator can see his deleted comments / replies.

1 solution

PHP
$secondsInDay = 86400
# Time as a float must greater or equal to zero and less or equal to one.
$dayAsFloat = 0.555

# Determine the number of seconds
$totalSeconds = intval($secondsInDay * $dayAsFloat)

# Calculate number of seconds
$seconds = $totalSeconds % 60
$totalSeconds = $totalSeconds / 60

# Calculate number of minutes
$minuntes = $totalSeconds % 60
$totalSeconds = $totalSeconds / 60

# Calculate number of hours
$hours = $totalSeconds % 60

$timeString = sprintf("%'0#2d:%'0#2d:%'0#2d", $hours, $minutes, $seconds)


This will leave you with a time value where the hours run from 00 - 23. If you want AM/PM time you'd have to adjust for that.

Cheers!

—MRB
 
Share this answer
 
Comments
[no name] 10-Aug-11 1:05am    
Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900