michaelmcgurk
michaelmcgurk

Reputation: 6511

Convert 12hour clock to 24hour clock PHP

I would really love some help with this.

Currently when I use the following PHP, my output is 02:42. What I would like is 14:42.

<?php 

date_default_timezone_set('Europe/London'); 

echo date("h:i",time()); 

?>

Also, is it possible to then use the converted time value to create a conditional statement to say if greater than 10am, do this.

Many thanks for any pointers

Upvotes: 1

Views: 2059

Answers (7)

ranojan
ranojan

Reputation: 837

This will convert to 24 hours clock to 12 hours clock

    $date='17:29';
    echo  date ('g:i a',strtotime($date));
    output: 5:29 pm

This will convert to 12 hours clock to 24 hours clock

    $date='5:29';
    echo date ('H:i',strtotime($date));
    output: 17:29

Upvotes: 0

Eugen Rieck
Eugen Rieck

Reputation: 65332

echo date("H:i",time()); 

Will give you 24-based hours.

For the second Problem you could use:

if(date("a") == "pm" OR date("g") > 10){ ... }

Upvotes: 5

Ivanka Todorova
Ivanka Todorova

Reputation: 10229

Use one of these: G - 24 hour c (0-23) H - 2 digit 24 hour (00-23)

echo date("H:i", time());

Also to check

$hour = date("H", time());

if($hour > 10)
{
//do stuff
}

Upvotes: 3

Rambo
Rambo

Reputation: 248

try

date("H:iA",time());

the A, will give you AM or PM

Upvotes: 0

Jason Zambouras
Jason Zambouras

Reputation: 182

I think all you need to do is change the lower case h into an upper case H

all the formats you can use are here http://php.net/manual/en/function.date.php

Upvotes: 2

Marmelatze
Marmelatze

Reputation: 188

you can accomplish that with:

date("H:i",time());

Upvotes: 5

Emyr
Emyr

Reputation: 2371

Use "H:i" for 24h time.

https://www.php.net/manual/en/function.date.php

Upvotes: 9

Related Questions