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')
Related
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 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'));
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]);
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).