Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert 12 hour clock time into 24 hour clock time in PHP?

Tags:

php

datetime

I am using following function and I want time in 24hr clock format but this gives me time in 12hrs:

<?php
date_default_timezone_set('Asia/Kolkata');
$timestamp =  date("d/m/Y h:i:s", time());
print $timestamp ;
?>

What am I doing wrong?

like image 201
MySQL Rocks Avatar asked Jun 22 '10 11:06

MySQL Rocks


People also ask

How do you convert 12-hour to 24 hour time?

For example if the time in 12 hour clock is 11:00 PM, then the time in 24 hour clock is 11:00 + 12:00 = 23:00 hours. Q.

How can change time in 24 hour format in PHP?

php //current Date, i.e. 2013-08-01 echo date("Y-m-d"); //current Time in 12 hour format, i.e. 08:50:55pm echo date("h:i:sa"); //current Time in 24 hour format, i.e. 18:00:23 echo date("H:i:s"); //current Month, i.e. 08 echo date("m"); //current Month name, i.e. Aug echo date("M"); ?>

How do you make a 24 hour clock format?

In Windows, click the Start button. Click Control Panel, and then click Clock, Language, and Region. Under Regional and Language, click Change the date, time, or number format.


2 Answers

From the docs for date(): The H format character gives the hour in 24h format. Also, you can use G if you do not want the leading 0 for hours before noon.

Examples (if current time was seven-something-AM)
date('H:i:s') -> "07:22:13"
date('G:i:s') -> "7:22:13"


For your specific case:

$timestamp = date("d/m/Y H:i:s", time());
like image 51
jensgram Avatar answered Sep 24 '22 04:09

jensgram


According to the manual the difference is in the capitalization of hour portion: "h" returns a 12-hour format of an hour with leading zeros, 01 through 12. "H" returns a 24-hour format of an hour with leading zeros, 01 through 23.

like image 44
Webdezyner Avatar answered Sep 20 '22 04:09

Webdezyner