Issues with Time Array in PHP

Posted on 2009-02-10
Medium Priority
Last Modified: 2013-12-12

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.

$times = array();
$cnt = 0;
for ($i = 0; $i < 24; $i++)
	$h = $i % 12;
	if ($i < 12)
		$x = "AM";
	 	$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;
	$name = "$h:30 $x";
	$times[$cnt]['id'] = $name;
	$times[$cnt]['name'] = $name;

Open in new window

Question by:ctoolz
  • 2
LVL 19

Accepted Solution

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

LVL 19

Expert Comment

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


Assisted Solution

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

Open in new window


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