How to create dd/mm/yyyy date format using DateTime - php

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

Related

Carbon 12 hous format

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')

Creating DateTime object with DateTime::createFromFormat() fails

I have been given a date string from an API in the format e.g.
20120522T143127
I am trying to convert this to a DateTime object, but the object creation fails because of the 'T' (I think).
This is my current code:
$date = DateTime::createFromFormat( "YMDTHis", '20120522T143127' );
$result = $date->format( $format );
What am I missing?
I also tried:
$date = DateTime::createFromFormat( "YMD\THis", '20120522T143127' );
Your format is a bit wrong:
M A short textual representation of a month, three letters Jan through Dec
Use m instead of M
m Numeric representation of a month, with leading zeros 01 through 12
D A textual representation of a day, three letters Mon through Sun
Use d instead of D
d Day of the month, 2 digits with leading zeros 01 to 31
Just escape the T with a backslash
So just use:
Ymd\THis
echo date('Y-m-d H:i:s',strtotime('20120522T143127'));

PHP Date Conversion to Timestamp

I am trying to convert the following string (or strings of this time) to timestamps:
Closing date: 02 Apr 15
Closing date: 06 May 15
My code is as follows:
$start_date = explode("Closing date: ", $string);
$start_date = DateTime::createFromFormat('DD M yy', $start_date[1]);
But when I try echoing $start_date->getTimestamp() it tells me
Fatal error: Call to a member function getTimestamp() on a non-object
Any idea on what I may be doing wrong? DD M yy seems like the right date format to use.
It should be:
$start_date = explode("Closing date: ", $string);
$start_date = DateTime::createFromFormat('d M y', $start_date[1]);
The format you need to pass to DateTime::createFromFormat() to help it parse your data is d M y. I extracted the significance of the letters from the documentation:
d and j: Day of the month, 2 digits with or without leading zeros (01 to 31 or 1 to 31);
F and M: A textual representation of a month, such as January or Sept (January through December or Jan through Dec);
y: A two digit representation of a year (which is assumed to be in the range 1970-2069, inclusive); Examples: 99 or 03 (which will be interpreted as 1999 and 2003, respectively).
You need to change 'DD M yy' a to a right date format "d M y"
$start_date = explode("Closing date: ", 'Closing date: 02 Apr 15');
$start_date = DateTime::createFromFormat('d M y', $start_date[1]);
var_dump($start_date)
object(DateTime)[3430]
public 'date' => string '2015-04-02 15:27:28.000000' (length=26)
public 'timezone_type' => int 3
public 'timezone' => string 'Europe/London' (length=13)
DD M yy is actually not the right format. Taken from the DateTime::createFromFormat docs:
D and l - A textual representation of a day - Mon through Sun or Sunday
through Saturday
Try doing a var_dump of $start_date before calling getTimestamp, I bet is's actually false:
Returns a new DateTime instance or FALSE on failure.
The format you need to use is d M y.
Why not simply convert date to timestamps format using strtotime
$string="Closing date: 02 Apr 15";
$start_date = explode("Closing date: ", $string);
print strtotime($start_date[1]);

php not friendly with date format d/m/Y

I can't convert date to format d/m/Y.
date("d-m-Y", strtotime(substr($code, 22, 6)) ); return 21-10-2014
but date("d/m/Y", strtotime(substr($code, 22, 6)) ); return 21/10/2014
How to format?
I would use DateTime to create an object with your format, and then reformat it.
<?php
$date = DateTime::createFromFormat('ymd', '140521');
echo $date->format('d/m/y'); //Output: 21/05/14
https://eval.in/208227
How would you like your formated date be displayed? The d, m and y in "d-m-Y" or "d/m/Y" are responsible for what you wish to display, not - or /
Here is a few list of parameters and their output:
d : Day of the month, 2 digits with leading zeros 01-31
m : Numeric representation of a month, with leading zeros 01-12
Y : A full numeric representation of a year, 4 digits
F : A full textual representation of a month, such as January or March
l : A full textual representation of the day of the week Sunday through Saturday
If you want to display 21 October 2014, you will have to use:
date("d F Y", strtotime(substr($code, 22, 6)) );
$originalDate = "2014-10-21";
$newDate = date("d/m/Y", strtotime($originalDate));
(see strtotime and date docs on the PHP site).

Formatting the response of date()

The function date("y-M-d H:m:s") returns 12-Jul-23 19:07:35.
But I need it to return 2012-07-23 19:07:35.
How do write 07 instead of Jul and 2012 instead of 12 (yyyy does no do this).
this should work:
date("Y-m-d H:m:s")
http://php.net/date
Y A full numeric representation of a year, 4 digits
m Numeric representation of a month, with leading zeros
d Day of the month, 2 digits with leading zeros
I believe it is date('Y-m-d H:m:s')

Categories