?
Solved

Issues with Time Array in PHP

Posted on 2009-02-10
3
Medium Priority
?
212 Views
Last Modified: 2013-12-12
Hello,

We have a form that has a couple of time fields. I need the form drop down to display in 12 hour time, but upload to the database as 24 hours.

I have attached the array that is used and I was hoping someone could point me in the right direction.

Thanks
$times = array();
$cnt = 0;
for ($i = 0; $i < 24; $i++)
{
	$h = $i % 12;
	if ($i < 12)
		$x = "AM";
	else
	 	$x = "PM";
	if ($h == 0) $h = 12;
	if ($h < 10) $h = "0" . $h;
	$name = "$h:00 $x";
	$times[$cnt]['id'] = $name;
	$times[$cnt]['name'] = $name;
	$cnt++;
	$name = "$h:30 $x";
	$times[$cnt]['id'] = $name;
	$times[$cnt]['name'] = $name;
	$cnt++;
}

Open in new window

0
Comment
Question by:ctoolz
  • 2
3 Comments
 
LVL 19

Accepted Solution

by:
Michael701 earned 1000 total points
ID: 23605081
I think you want this for your option output
no need for the array stuff

for ($i = 0; $i < 24; $i++)
{
  echo "<option value='".gmdate('H:i',($i*3600))."'>".gmdate('h:i A',$i*3600) ."</option>\n";
  echo "<option value='".gmdate('H:i',($i*3600+1800))."'>".gmdate('h:i A',$i*3600+1800) ."</option>\n";
}

Open in new window

0
 
LVL 19

Expert Comment

by:Michael701
ID: 23605124
duh, even better is this
for ($i = 0; $i < 24; $i=$i+.5)
  echo "<option value='".gmdate('H:i',($i*3600))."'>".gmdate('h:i A',$i*3600) ."</option>\n";

Open in new window

0
 
LVL 8

Assisted Solution

by:agamal
agamal earned 1000 total points
ID: 23639856
use data() as follow ...
<?php
echo date("H:i:s");
echo "<br>";
echo date("h:i:s");
?>

Open in new window

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question