Jump to content

Recommended Posts

Good day experts!! I would like to ask if is it possible in a date() function to determine that it will only change to PM or AM if it is already 1 o clock?

 

for example:

 

if the time now is 12AM, the next hour will be 1PM then after 12hours, it will be 1AM but before 1AM, it is still 12PM...

I'm just concern about the transition from AM to PM...

 

Thanks!! cheers!!

Link to comment
https://forums.phpfreaks.com/topic/83584-solved-question-about-date-time/
Share on other sites

date() requires a timestamp of some form... whether given directly or created with mktime(). No timestamp supplied implies the current time timestamp. The timestamp always knows whether AM or PM in 12 hour formatted displays, so if you include the AM/PM indicator, it will always be accurate.

 

PhREEEk

the switch from AM to PM or PM to AM is made instantly, not after an hour has elapsed

 

If you run this script, which increments the time by 1 second

 

<?php

echo "<h3>At Midday</h3>";
$start = mktime (11,59,55);

for ($i=0; $i<10; $i++) echo date ('g:i:s A', strtotime("+$i seconds", $start)), '<br>';

echo "<h3>At Midnight</h3>";
$start = mktime (23,59,55);

for ($i=0; $i<10; $i++) echo date ('g:i:s A', strtotime("+$i seconds", $start)), '<br>';

?>

 

you get

[pre]At Midday

11:59:55 AM

11:59:56 AM

11:59:57 AM

11:59:58 AM

11:59:59 AM

12:00:00 PM

12:00:01 PM

12:00:02 PM

12:00:03 PM

12:00:04 PM

 

At Midnight

11:59:55 PM

11:59:56 PM

11:59:57 PM

11:59:58 PM

11:59:59 PM

12:00:00 AM

12:00:01 AM

12:00:02 AM

12:00:03 AM

12:00:04 AM

 

I don't why you want when it's clearly a wrong representation of the time, but you could fool it by subtracting an hour when outputting the am/pm bit

 

<?php

$time = strtotime('2007-12-29 12:30:00');

echo 'The time is ' . date ('g:i', $time) . date('A', strtotime('-1 hours', $time));


?>

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.