Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
| Search | Today's Posts | Mark Forums Read
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 10th, 2008, 03:16 PM
Authorized User
 
Join Date: Nov 2007
Location: , , .
Posts: 54
Thanks: 1
Thanked 0 Times in 0 Posts
Default converting HH:MM to HH.MM

Do we have a pre-defined function in PHP to convert from time to decimal value? For example if a user enters 2:30 or 2.5, it should be displayed as 2.5 in both of the cases.
If we cannot handle it through PHP, then is there anything in javascript to do the same task ?
Any help would be greatly appreciated,

Thanks,

 
Old July 10th, 2008, 07:02 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

JS is easy enough.

But how did you get the HH:MM in the first place?? Is it just coming from some form field?

<form>
<input name="time" onChange="convertTime(this);">
</form>

<script>
function convertTime( what )
{
    var re = /^\d\d?\:\d\d$/;
    if ( re.test(what.value) )
    {
        var temp = val.split(":");
        hours = parseFloat( temp[0] );
        mins = parseFloat( temp[1] );
        what.value = hours + mins/60.0;
    }
}
</script>

That won't change the value entered unless it conforms to the regexp pattern given. Either h:mm or hh:mm

If you want to allow other patterns, you have to say so.
 
Old July 10th, 2008, 08:54 PM
Authorized User
 
Join Date: Nov 2007
Location: , , .
Posts: 54
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Thanks for the help, appreciate that.
yes, the input is coming from a field. A user is required to enter hours in decimal, but the user should also have an option of entering the value in hours. Example 2.5 or 2:30.
I was thinking of a way in PHP to convert the input to decimals in both cases and go from there. With javascript I have to perhaps add a function that can take the entered value and do the conversion. I will try with javascript as you have suggested, but I would be happy to find a solution in PHP as well.
Thanks again!!!

 
Old July 11th, 2008, 08:10 AM
Authorized User
 
Join Date: Nov 2007
Location: , , .
Posts: 54
Thanks: 1
Thanked 0 Times in 0 Posts
Default

...the code is not functional, not working.

 
Old July 11th, 2008, 09:08 AM
Authorized User
 
Join Date: Nov 2007
Location: , , .
Posts: 54
Thanks: 1
Thanked 0 Times in 0 Posts
Default

this one works fine:
Code:
<form name ="testform">
<input type = "text" name="time" onChange="convertTime()">
</form>

<script>
function convertTime( )
{
    alert("funtion...")
     alert(document.testform.time.value)
    var re = /^\d\d?\:\d\d$/
    if ( re.test(document.testform.time.value) )
    {
        alert("if...")
        var temp =document.testform.time.value.split(":")
        alert(temp[0])
        alert(temp[1])
        hours = parseFloat( temp[0] )
        mins = parseFloat( temp[1] )
       document.testform.time.value = hours + mins/60.0
    }
     alert("out...")
}
</script>
 
Old July 11th, 2008, 01:33 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

I give up. What did you change????

The only difference I see is that instead of passing in a reference to the form field using
    <input name="time" onChange="convertTime(this);">
and then receiving the reference in the function via
    function convertTime( what )

you chose to use
    document.testform.time.value

But those really are 100% equivalent. Did you perhaps forget to use this when you tried my version???




Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert seconds to HH:MM:SS bph SQL Server 2000 11 July 31st, 2009 01:53 PM
Convert time in seconds to hh:mm:ss 41509331 C++ Programming 0 March 18th, 2008 04:43 AM
Converting number to hh:mm dlyles Beginning VB 6 8 September 19th, 2006 12:27 PM
Convert Seconds to HH:MM:SS jpillonel Crystal Reports 0 December 17th, 2003 09:20 AM
validation of [HH]:mm beastwood_uk Excel VBA 3 August 10th, 2003 08:37 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.