Strtotime() doesn't work with dd/mm/YYYY format
Here is the simplified solution:
$date = '25/05/2010';
$date = str_replace('/', '-', $date);
echo date('Y-m-d', strtotime($date));
Result:
2010-05-25
The strtotime
documentation reads:
Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed.
PHP strtotime() function that accepts a format?
If you have PHP 5.3:
$date = DateTime::createFromFormat('d/m/Y H:i:s', '03/05/2011 16:33:00');
echo $date->getTimestamp();
get strtotime of specific time in php
If you're trying to get a timestamp of today at 8pm, it's actually much more simple than using date
since you can use relative times in a strtotime
:
$curtime = strtotime('today 8pm');
If you test this with date
:
echo date('Y-m-d H:i:s', $curtime); // Returns 2014-12-17 20:00:00
Here's a whole writeup on relative date formats that explains how you can construct proper relative dates.
strtotime() DateTime class analog method
That would be DateTime::__construct
:
$date = new DateTime('Sunday');
PHP strtotime(): date showing '1970-01-01 ' after conversion
Your format is not a format that the parser understands.
In your case 13
is not a "month". So the parser doesn't understand to date.
You should use DateTime::createFromFormat()
:
$date = DateTime::createFromFormat('m-d-Y H:i:s','04-13-2018 0:00:53');
echo $date->format('Y-m-d H:i:s');
Output:
2018-04-13 00:00:53
Note that the format could also be: 'm-d-Y G:i:s'
with G
for "24-hour format of an hour without leading zeros".
strtotime With Different Languages?
From the docs
Parse about any English textual datetime description into a Unix
timestamp
Edit: Six years down the road now, and what was meant to be a side-note about why strtotime() was an inappropriate solution for the issue at hand became the accepted answer /p>
To better answer the actual question I want to echo Marc B's answer: despite the downvotes, date_create_from_format, paired with a custom Month interpreter will provide the most reliable solution
However it appears that there is still no silver-bullet for international date parsing built-in to PHP for the time being.
How to exactly use PHP strtotime with 'this monday' combined with a time?
Try this:
$next_valid_date_time = strtotime('this Monday 20:00');
if(time() > $next_valid_date_time) {
$next_valid_date_time = strtotime('Monday next week 20:00');
}
echo date('Y-m-d H:i', $next_valid_date_time);
PHP strtotime EST back and forth conversion
I'd say that this is because currently New York is on daylight savings time - EDT rather than EST. This affects things like so:
date_default_timezone_set("America/New_York");
strtotime("10/31/2012 7:30pm"); // translates to "Wed, 31 Oct 2012 19:30:00 -0400"
strtotime("10/31/2012 7:30pm EDT"); // translates to "Wed, 31 Oct 2012 19:30:00 -0400"
strtotime("10/31/2012 7:30pm EST"); // translates to "Wed, 31 Oct 2012 20:30:00 -0400"
The quick fix is probably to not add the timezone to the string, strtotime()
will use the correct default timezone you set.
You can be a bit more exact about how your date is being parsed by using the DateTime
createFromFormat
function:
$date = DateTime::createFromFormat('m/d/Y g:ia', "10/31/2012 7:30pm");
echo $date->format('U');
Alternatively if you wait until November the problem will resolve itself :-)
Related Topics
Import Class Conditionally with the Keyword 'Use'
How to Merge 3 Images into 1 Image via PHP
Date Function to Display All Dates Between Two Dates
PHP Curl with Curlopt_Followlocation Error
How to Use Bind_Param for Order By
How to Backup/Export the Connected Database Database.SQL File in Laravel
Differences Between a While Loop and a for Loop in PHP
"Transfer-Encoding: Chunked" Header in PHP
Indented List to Multidimensional Array
Why Does PHP's Call_User_Func() Function Not Support Passing by Reference
Why Don't PHP and JavaScript's Timestamps Match
PHP - Is There a Portable Version of PHPunit
Set Quantity Minimum, Maximum and Step at Product Level in Woocommerce
Codeigniter Command Line Error - PHP Fatal Error: Class 'Ci_Controller' Not Found