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]);
Related
I need to convert the date from 2020-11-01 to string 01 Nov 2020
So far I could achieved like this
$test = DateTime::createFromFormat('Y-m-d', '2020-11-01')->format('d m Y');
But the result is show 01 11 2020
How to make the month become Nov?
date('d M Y',strtotime('2020-11-01'));
you can use strtotime to get the time stamp and the use that time stamp to change format with date funtion
$timeStamp=strtotime("2020-11-01");
$res=date("d M Y",$timeStamp);
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 am trying to get next monday for a given date using strttotime but I am getting dates from Jan 1970 as ouput. Below is my code line which is written for the getting date of next monday, I got this code from here. Can anyone please help me understanding why this is happening. Thanks in advance.
Code:
$date_init = date('Y m d', strtotime('next monday', strtotime('2016 06 22')));
Expected Output:
2016 06 27
Actual Output:
1970 01 05
The string 2016 06 22 is not a valid date format according to the manual. Try to add hyphens:
$date_init = date('Y m d', strtotime('next monday', strtotime('2016-06-22')));
You can find all valid date formats here: http://php.net/manual/en/datetime.formats.date.php
Try this:
$date_init = date('Y m d', strtotime('next monday', strtotime('22-06-2016')));
'2016 06 22' needs to be formated to one of the formats mentioned here
2016 06 02 is not one of the accepted date formats as described in the PHP docs. For this reason, the inner call to strtotime returns FALSE as defined in the docs in case the given time string cannot be parsed.
Since this is not a valid input for the outer strtotime for the $now parameter, it takes the epoch, or January 1, 1970, as basis for it's calculations.
As a result, you end up with the next monday after January 1, 1970.
Removing the spaces from the initial date string will solve this:
$date_init = date('Y m d', strtotime('next monday', strtotime('20160622')));
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'));