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?
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");
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' );
Related
I have some dates in my DB and I want to show them in a 12 hour format with AM - PM at the end. I am using carbon, my code is this:
$hora = Carbon::createFromFormat('h:i A', $fecha_inicio, 'UTC')->setTimeZone($timeZone)->format('h:i A');
$fecha_inicio is something like 2018-11-02 13:47:03.
But this throws an error: ** Hour can not be higher than 12**
From the docs:
h 12-hour format of an hour with leading zeros 01 through 12
H 24-hour format of an hour with leading zeros 00 through 23
So, use H:i instead of h:i
$hora = Carbon::createFromFormat('Y-m-d H:i:s', $fecha_inicio, 'UTC')->setTimeZone($timeZone)->format('h:i A');
The first parameter from createFromFormat is the format you already have in $fecha_inicio
If you want the TIME in 12hr format
Carbon\Carbon::parse($fecha_inicio)->isoFormat('h:mm:i')
And if you want the TIME in 24hr format
Carbon\Carbon::parse($fecha_inicio)->isoFormat('H:MM:I')
I want to create a DateTime object from format dd/mm/yyyy H:i:s, then I type:
DateTime::createFromFormat(
'dd/mm/yyyy H:i:s',
'01/02/2018 00:00:00'
);
And the result is false. Why and how to fix it?
Check the PHP: DateTime::createFromFormat for proper formatting.
d will try to match 7 or 07 day
m will match 1 or 01 month
Y will match 2017 (four-digit year)
The format string you should be using is d/m/Y H:i:s
Corrected:
DateTime::createFromFormat(
'd/m/Y H:i:s',
'01/02/2018 00:00:00'
);
Your Question on Why
The reason why it happened, is you are trying to say that your date actually has multiple months, multiple days, and multiple years:
DateTime::createFromFormat(
'dd/mm/yyyy H:i:s',
'01/02/2018 00:00:00'
);
So that action is expecting your input date to look like this:
'0101/0202/18181818 00:00:00'
But since your date did NOT fit that bill, it will return false as it fails to match the date to the format you provided.
Your Request for a Fix
The way to fix it, would be this instead (note a single d m and Y as per the documentation of PHP.net):
DateTime::createFromFormat(
'd/m/Y H:i:s',
'01/02/2018 00:00:00'
);
You used format 'dd/mm/yyyy H:i:s' which is not correct.
Use single letters such as:
d to represent two digit day 01 to 31 or 1 to 31,
m to get two digit month 01 through 12
Y uppercase as four digit year 1977 or 2017
H upper case as two digit hours 00 through 23
i to get two digit minutes 00 to 59
s to get two digit seconds 00 through 59
So correct format is 'd/m/Y H:i:s'
http://php.net/manual/en/datetime.createfromformat.php
I try to convert time to my local time (Asia/Jakarta)
this is my Current Milliseconds : 1507539987576
i want convert to like this format : Mon Oct 09 16:06:27 WIB 2017
this is my code using php
date_default_timezone_set("Asia/Jakarta");
$now = new DateTime();
$nowadays = date('Y-m-d');
$time = round(microtime(true) * 1000);
$seconds = $time / 1000;
$currdate = date("Y-m-d H:i:s", $seconds);
echo $currdate;
and the result like this : 2017-10-09 16:06:27
can someone tell me how to convert the time like this format : Mon Oct 09 16:06:27 WIB 2017
The PHP documentation outlines all of the format characters. The string that you require to parse the date in the format you've indicated (i.e. Mon Oct 09 16:06:27 WIB 2017) is as follows:
date( 'D M d H:i:s T Y' );
We can break this down as follows:
D: A textual representation of a day, three letters
M: A short textual representation of a month, three letters
d: Day of the month, 2 digits with leading zeros
H: 24-hour format of an hour with leading zeros
i: Minutes with leading zeros
s: Seconds, with leading zeros
T: Timezone abbreviation
Y: A full numeric representation of a year, 4 digits
I also do not understand why you're working with microtime and then calculating the seconds. By default, date() will always use the current time, or you can simply use time() instead of your calculations.
I have a string $newAfter that outputs like Wednesday 9th of March 2016 11:59:59 PM.
I use this code to calculate the days left:
$daysleft = floor(($newAfter - time()) / 86400);
this is the result I get:
-16559
I want the days remaining up leading up to the date in string.
Of course you can't subtract days from that format string, you'll need to subtract them as integers of use DateTime objects:
$notAfter = 'Thu, 28 Apr 2016 03:22:56 +0200';
$today = new DateTime;
$date = DateTime::createFromFormat('D, d M Y H:i:s O', $notAfter);
$diff = $date->diff($today);
echo "{$diff->days} days left.";
Demo
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?
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());
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.
According to the manual, G or H give you the time in 24-hour format. You should read the manual.
date('H', time());
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.
// 24-hour time to 12-hour time
$time_in_12_hour_format = date("g:i a", strtotime("13:30"));
// 12-hour time to 24-hour time
$time_in_24_hour_format = date("H:i", strtotime("1:30 PM"));