Strtotime() Doesn't Work With Dd/Mm/Yyyy Format

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.

strtotime not working with mm-dd-yyyy

It sees strings with - in them as dd-mm-yyyy and / as mm/dd/yyyy.

See also this question and the comments on the documentation.

Possible solutions / workarounds:

  • on php 5.3, use date_create_from_format
  • on older php and not on windows, use strptime
  • if neither can be used, either replace the - to / when necessary, or use one of the regexes suggested you can find through the linked question.

Note however that at some time you do need to know what the format is to start with. Computers are not mindreaders. They can't, and never will be able to, distinguish between mm-dd-yyyy and dd-mm-yyyy in the overlap ranges (both mm and dd <= 12) if you don't provide the distinction.

strtotime for DD/MM/YYYY not working properly

The DateTime class can be used in situations like this - it has the useful method createFromFormat

    $date='10/09/19';   //dd/mm/yy
$new=DateTime::createFromFormat('d/m/y', $date )->format('Y/m/d');
echo $new; //2019/09/10

Issue with using strtotime on dd/mm/YYYY format

problem:

However I'm stuck at the first hurdle with converting my date to a UNIX code.

Solution:

To get unix timestamp you can do something like this:

$unixdatetime = DateTime::createFromFormat('d/m/Y', get_post_meta( $the_query->post->ID, 'wedding_date', true ))->getTimestamp();
echo $unixdatetime;

strtotime() returns current date with DD.MM.YY input - why?

string date ( string $format [, int $timestamp = time() ] )

Timestamp :- The optional timestamp parameter is an integer Unix timestamp that defaults to the current local time if a timestamp is
not given.

In other words, it defaults to the value of time().

So in your case, in line echo "12.07.16 - ".date("d.m.Y", strtotime("12.07.16"));

Format you were suppose to put was 12.07.2016 but you put 12.07.16 which is discarded so it took the default local date

You can do like this to format

$datetime = DateTime::createFromFormat('d.m.y', '12.07.16');
echo $datetime->format('d.m.Y');

PHP convert date format dd/mm/yyyy = yyyy-mm-dd

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. Check more here.

Use the default date function.

$var = "20/04/2012";
echo date("Y-m-d", strtotime($var) );

EDIT I just tested it, and somehow, PHP doesn't work well with dd/mm/yyyy format. Here's another solution.

$var = '20/04/2012';
$date = str_replace('/', '-', $var);
echo date('Y-m-d', strtotime($date));

strtotime was not working in php with m-d-Y format in php?

Try this one:

$date_time = '05-23-2017 01:00';

$newdatetime = str_replace('-', '/', $date_time);

echo date('Y-m-d H:i:s', strtotime($newdatetime));

// Output: 2017-05-23 01:00:00

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.

Datepicker dd/mm/yy php and mysql error

1) You can show convenient Format to user end But when you store it in database Mysql date Format should be 2017-05-24 'YYYY-MM-DD'

JS

$( ".datepicker" ).datepicker({ dateFormat: "dd/mm/yy" });

2) And strtotime will not accept this date format 24/05/2017 seperated by slash so use string replace. and then change the format like this

PHP

$date = str_replace('/', '-', $_POST['dateStart']);
$date = date('Y-m-d', strtotime($date));


Related Topics



Leave a reply



Submit