54120

strtotime() does not return correct value when specifying date in dd/mm/yyyy format

I want to convert date 24/09/2010 in format dd/mm/yyyy to 2010-09-24 in format yyyy-mm-dd.

This works:

date("Y-m-d",strtotime("09/24/2010"));

But this does not:

date("Y-m-d",strtotime("24/09/2010")); // it returns '1970-01-01'

Any idea why?

Answer1:

according to php, the valid php formats are stated here. So basically what you gave is invalid.

Alternatively, you can use mktime, date_parse_from_format or date_create_from_format

Answer2:

strtotime does its best to guess what you mean when given a string, but it can't handle all date formats. In you example, it is probably thinking that you are trying to refer to the 24th month, which isn't valid, and returns 0, which date then treats as the unix epoch (the date you got).

you can get around this using the mktime() and explode() functions, like so:

$date = "24/09/2010"; $dateArr = explode("/",$date); $timeStamp = mktime(0,0,0,$dateArr[1],$dateArr[0],$dateArr[2]); $newFormat = date("Y-m-d",$timeStamp);

Answer3:

As you say, date("Y-m-d",strtotime("09/24/2010")) will work,because the date format--"09/24/2010"is correct, but "24/09/2010" is not the correct date format. you can find something useful here

Recommend

  • Last Modified of file downloaded does not match its HTTP header
  • PHP Countdown Timer Problem?
  • display todays bookings from mysql mrbs calendar
  • Most efficient way to convert a ISO Date into Unix timestamp?
  • How to save a large fractal image with the least possible memory footprint?
  • Can I run Robotium tests automatically in parallel on multiple emulators?
  • Restructure php contact form
  • Embed multiple jpeg images into EXCEL programmatically?
  • How to use java.time.LocalDate on a Play Framework JSON Rest?
  • How to convert list into string with quotes in python
  • jQuery: click function bind in for-loop with closure fix
  • iOS Date formatting
  • How To Delete All Words After X Characters
  • Differences between drawing an Ellipse in Android and Java
  • Whats the right place for testhelper-classes? (phpunit/best practise)
  • JSON data through JS/AJAX into PHP
  • Ruby and class variables in inherit class
  • calculating number of bytes of each row in an image
  • Insertion large number of Entities into SQL Server 2012 [duplicate]
  • Most efficient way to move table rows from one table to another
  • CakePHP ACL tutorial initDB function warnings
  • Spring Cloud Microservice Architecture Confusion
  • Unable to decode certificate at client new X509Certificate2()
  • Exception “firebase.functions() takes … no argument …” when specifying a region for a Cloud Function
  • Get data from AJAX - How to
  • How to delay loading a property with linq to sql external mapping?
  • Using variable in a value field in jMeter
  • Read text file and split every line in MSBuild
  • C# - Serializing and deserializing static member
  • Apache 2.4 and php-fpm does not trigger apache http basic auth for php pages
  • Java applet as stand-alone Windows application?
  • How can I use Kendo UI with Razor?
  • Upload files with Ajax and Jquery
  • Release, debug version and Authorization Google?
  • Do I've to free mysql result after storing it?
  • Numpy divide by zero. Why?
  • Is there a mandatory requirement to switch app.yaml?
  • A cron job substitute?
  • json Serialization in asp
  • Error creating VM instance in Google Compute Engine