Jump to content

[SOLVED] Time Formatting


kemper

Recommended Posts

I have display of:

<tr>
<td width='124' valign='top'>
<p style='margin-top: 0; margin-bottom: 0'><b>Date:</b></td>
<td width=216' valign='top'>
<p style='margin-top: 0; margin-bottom: 0'>".$data['date']=date("D, F j, Y",strtotime($date['date']))."</td>
</tr>
<tr>
<td width='124' valign='top'>
<p style='margin-top: 0; margin-bottom: 0'><b>Time/Notes:</b></td>
<td width='216' valign='top'>
<p style='margin-top: 0; margin-bottom: 0'>".$data['time']."</td>
</tr>

 

The time displays as 00:00:00, since mysql field is set up as:

`time` time NOT NULL default '00:00:00'

 

How can I change this to display as hh(12 hr):mm am/pm (%I:%M %p)?

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/53993-solved-time-formatting/
Share on other sites

Tht did not work since Date and Time are different fields within my database. I only need formatting of my time field:

`time` time NOT NULL default '00:00:00'

 

I also only want the Time edittable by my associates, thus why Date and Time are separate fields.

 

That code read the time from Date field.

 

Any suggestions?

I believe the mysql time field only allows the hous and minutes and not the am/pm part. You'd have to save it as the correct time in the database (without am/pm) then add the am/pm with an if statement when outputting..

something like:

<?php
$time = explode(':', $data['time']);

if($time[0] >= '12' && $time[0] < '00'){
     echo 'pm';
}else{
     echo 'am';
}
?>

a half assed way of doing it but this should work:

 

http://us2.php.net/manual/en/function.mktime.php

//int mktime ( [int $hour [, int $minute [, int $second [, int $month [, int $day [, int $year [, int $is_dst]]]]]]] )
<?php
$time = split(":", $data['time']);
$new_time = mktime($time[0], $time[1], $time[2], 1, 1, 1999);
echo "<p style='margin-top: 0; margin-bottom: 0'>'".date("h:i a", $new_time)."'</td>";
?>

 

un-tested but should work.

I do not see why it is not working I just tested it:

 

<?php

//int mktime ( [int $hour [, int $minute [, int $second [, int $month [, int $day [, int $year [, int $is_dst]]]]]]] )
$time = split(":", "08:30:00");
print_R($time);
$new_time = mktime($time[0], $time[1], $time[2], 1, 1, 1999);
echo "<p style='margin-top: 0; margin-bottom: 0'>'".date("h:i a", $new_time)."'</td>";

//int mktime ( [int $hour [, int $minute [, int $second [, int $month [, int $day [, int $year [, int $is_dst]]]]]]] )
$time = split(":", "23:30:00");
print_R($time);
$new_time = mktime($time[0], $time[1], $time[2], 1, 1, 1999);
echo "<p style='margin-top: 0; margin-bottom: 0'>'".date("h:i a", $new_time)."'</td>";

?>

 

Output:

Array

(

    [0] => 08

    [1] => 30

    [2] => 00

)

<p style='margin-top: 0; margin-bottom: 0'>'08:30 am'</td>

 

Array

(

    [0] => 23

    [1] => 30

    [2] => 00

)

<p style='margin-top: 0; margin-bottom: 0'>'11:30 pm'</td>

 

Are you sure the $time['time'] is coming out of the database right ???

My original statement was:

<p style='margin-top: 0; margin-bottom: 0'>".$data['time']."</td>

 

So, I am confused when you have statements including ... $time['time'] ...

 

And, the '".<b>date</b>("h:i a", $new_time)."' confuses me since it is not a date field.

 

This is all new to me.

 

Sorry, someoen else posted it in code I used.

 

<?php
//int mktime ( [int $hour [, int $minute [, int $second [, int $month [, int $day [, int $year [, int $is_dst]]]]]]] )
$time = split(":", $data['time']);
$new_time = mktime($time[0], $time[1], $time[2], 1, 1, 1999);
echo "<p style='margin-top: 0; margin-bottom: 0'>'".date("h:i a", $new_time)."'</td>";
?>

 

Try that out.

 

I would of hoped you might of been able to figure out how to modify it, but sometimes hoping is too much I guess.

OK,  I changed it and received:

Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /home/kemper/public_html/***/scheduling.php on line 29

 

Parse error: syntax error, unexpected T_STRING in /home/kemper/public_html/***/scheduling.php on line 29

 

Code is:

opentable ("Fall Schedules");
print $data['time']\n;
closetable ();
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.