We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now


Issues with Time Array in PHP

ctoolz asked
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

Watch Question

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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
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

use data() as follow ...
echo date("H:i:s");
echo "<br>";
echo date("h:i:s");

Open in new window

Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.