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
Share on other sites

I am not sure how to code that for time.

 

I tried unsuccessfully as:

<p style='margin-top: 0; margin-bottom: 0'>'".$data['time']=time("D, F j, Y h:i a",strtotime($time['time']))."'</td>

 

Suggestions would be greatly appreciated.

Link to comment
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?

Link to comment
Share on other sites

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';
}
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 ???

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

prolly coding (user) error:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/kemper/public_html/***/scheduling.php on line 30

Link to comment
Share on other sites

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 ();
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.