954,574 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

24 Hours to 12 Hours in PHP

How we can use current time instead of '15:33:56'
now() Function or time()
but it gives wrong time in AM and PM.

<?php


function twentyfourToTwelve($time)
{
    return date('g:i:sa', strtotime($time));
}


 /*** Example usage ***/
 echo twentyfourToTwelve('15:33:56').'';

 echo twentyfourToTwelve('15:33').'';

 echo twentyfourToTwelve('15.12').'';

 echo twentyfourToTwelve('2:33:56').'';

 echo twentyfourToTwelve('15:33').'';
?>
ayesha789
Posting Pro in Training
496 posts since Jun 2009
Reputation Points: 17
Solved Threads: 7
 

Please tell me how to show time in !2 Hours Format.
Thanks.

ayesha789
Posting Pro in Training
496 posts since Jun 2009
Reputation Points: 17
Solved Threads: 7
 

How's this?

date("g:i:s a", strtotime("{$time} UTC"))

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

What u said?
Please explain Dear.

ayesha789
Posting Pro in Training
496 posts since Jun 2009
Reputation Points: 17
Solved Threads: 7
 
function twentyfourToTwelve($time)
{
    return date('g:i:s a', strtotime($time . " UTC"));
}
 
 
 /*** Example usage ***/
 echo twentyfourToTwelve('15:33:56');


This work?

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

This show 8:33:56 pm in result.
Its wrong.
I need to show time in my website in 12 Hours Format so please tell me.

ayesha789
Posting Pro in Training
496 posts since Jun 2009
Reputation Points: 17
Solved Threads: 7
 

What is wrong with ardav's post?
12 Hour format is 01:01:01 PM
24 Hour format is 13:01:01

__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

Congrats adatapost
i want to show time on my website .
pakistan standard time with 12 hours time.
please tell me how to.

ayesha789
Posting Pro in Training
496 posts since Jun 2009
Reputation Points: 17
Solved Threads: 7
 

Please tell me about Showing Time in 12 Hours Format.

ayesha789
Posting Pro in Training
496 posts since Jun 2009
Reputation Points: 17
Solved Threads: 7
 

For **** sakes Ayesha, if you'd said you wanted Pakistan time, that would have been straight forward - we ain't got no crystal balls you know. Look at the php documentation on setting times. Sheesh...

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

Okz, Sorry

ayesha789
Posting Pro in Training
496 posts since Jun 2009
Reputation Points: 17
Solved Threads: 7
 

I got the solution using Javascript. Thanks.

ayesha789
Posting Pro in Training
496 posts since Jun 2009
Reputation Points: 17
Solved Threads: 7
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You