RE: Time input formatting in PHP-12 hour clock

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



There are several ways you can do it. But the first step is to translate to
24 hour time.

So the first thing we have to check is if the am or pm flag was set. Then
add 12 hours or leave as is OR set the hour to 00 (midnight).

Something like this may help:

Switch(strtoupper($ampm_flag)) {
// if it's am all we have to worry about is midnight because all hours
before noon in 12 hour time //are the same as 24 except midnight.
Case "AM": {
		If ($hour = 12) {
			$hour = 0;
}
break;
}
case "PM": {

/* now we gotta check to see if the pm time is smaller that 12 if so add 12
hours if not leave it. Because the only thing that is the same in the
afternoon between 12 and 24 hour time is 12 PM */

	if ($hour < 12) {
		$hour += 12;
	}
}

/* now sprintf into mysql format. I'm more familiar with PERL's sprintf so
check the PHP docs and make sure I got this right */

$mysqldate = sprintf ("%04d-%02d-%02d %02d:%02d:%02d",
 $year, $month, $day, $hour, $minute, $second);

/*Then insert $mysqldate into your date field. You could also concatinate,
just make sure that your digits are the right length, you could also check
the mysql manual for the different formats of inserting datetimes. There are
less friendly ways of inserting datetimes. */

/* by the way this was sort of done on the fly and was not tested. If anyone
encounters any bugs, let me know */


Carl.

-----Original Message-----
From: David Shugarts [mailto:Azimuth@CompuServe.com]
Sent: Friday, June 13, 2003 10:27 AM
To: php-db@lists.php.net
Subject:  Time input formatting in PHP-12 hour clock

Hi, All--

Has anyone already solved the problem of how to get a time input from the
user in people time (12-hour clock) and format it to be INSERTed in a mySQL
time field?

The mySQL time field is 24-hour and of course it can be formatted in the
SELECT statement to be displayed as 12-hour for the user. But--presumably in
PHP--I need the user to be able to input a time, then validate it as to
whether it is complete (e.g., expresses AM or PM and evaluates correctly),
then pass it into an INSERT statement into the time field of a database.

I suspect this already exists, just can't find an example.

TIA

Dave Shugarts


--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux