Date and time in 24 hours format

105,183

Solution 1

Use H instead:

$test = 'Fri, 15 Jan 2016 15:14:10 +0800';
$t = date('Y-m-d H:i:s',strtotime($test));
echo $t;

H: 24-hour format of an hour with leading zeros 00 through 23

G should be the same, but without leading zeroes though. I suspect that your PHP is set to a different timezone than +0800. Can you confirm your timezone (date_default_timezone_get())?

EDIT

OP confirmed that his timezone was set to UTC, in which case it maskes perfect sense that it shows 7 in the morning, as date uses PHPs default timezone.

If you want to "inherit" the Timezone, while getting more flexibility, you should switch to DateTime:

echo (new DateTime($test))->format("Y-m-d H:i:s");

Solution 2

https://www.w3schools.com/php/php_date.asp

Get a Date

The required format parameter of the date() function specifies how to format the date (or time).

Here are some characters that are commonly used for dates:

  • d - Represents the day of the month (01 to 31)
  • m - Represents a month (01 to 12)
  • Y - Represents a year (in four digits)
  • l (lowercase 'L') - Represents the day of the week

Get a Time

Here are some characters that are commonly used for times:

  • H - 24-hour format of an hour (00 to 23)
  • h - 12-hour format of an hour with leading zeros (01 to 12)
  • i - Minutes with leading zeros (00 to 59)
  • s - Seconds with leading zeros (00 to 59)
  • a - Lowercase Ante meridiem and Post meridiem (am or pm)

https://www.php.net/manual/en/datetime.format.php

public DateTime::format ( string $format ) : string

<?php

    $datetime = new DateTime( "now", new DateTimeZone( "Europe/Bucharest" ) );

    echo $datetime->format( 'Y-m-d H:i:s' );
Share:
105,183

Related videos on Youtube

Pathik Vejani
Author by

Pathik Vejani

successful achiever.

Updated on December 25, 2020

Comments

  • Pathik Vejani
    Pathik Vejani over 3 years

    I have a date in this format Fri, 15 Jan 2016 15:14:10 +0800, and I want to display time like this 2016-01-15 15:14:10.

    What I tried is:

    $test = 'Fri, 15 Jan 2016 15:14:10 +0800';
    $t = date('Y-m-d G:i:s',strtotime($test));
    echo $t;
    

    But it is displaying date in this format: 2016-01-15 7:14:10, it should be 2016-01-15 15:14:10.

    How can i do this?

    • Mark Baker
      Mark Baker over 8 years
      What is the timezone setting on your server?
  • Pathik Vejani
    Pathik Vejani over 8 years
    date_default_timezone_get() displyas only UTC
  • Pathik Vejani
    Pathik Vejani over 8 years
    i also tried H but no luck, displaying 2016-01-15 07:14:10
  • rdiz
    rdiz over 8 years
    That's why. You're inputting a date from a timezone that is 8 hours after UTC, in which case it makes perfectly sense that it corresponds to 7 in the morning at your local time.
  • Pathik Vejani
    Pathik Vejani over 8 years
    So what i have to do now? How can i set ?
  • rdiz
    rdiz over 8 years
    I didn't downvote, so I don't know. Probably because it appears like you didn't put that much effort into researching or consulting the PHP manual before you created a question here.