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

x

Issues with Time Array in PHP

ctoolz
ctoolz asked
on
Medium Priority
227 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

Comment
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

Commented:
use data() as follow ...
<?php
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.

OR

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.