PHP's simple time format does not work!

I have the following meaning:

30/05/2010 @ 09:15:15 

I need to convert it to Ymd H: i: s.

I tried:

 $date = "30/05/2010 @ 09:15:15"; $formatteddate = date("Ymd H:i:s", time($date)); echo $formatteddate; 

In the end, I got a value dated 1970. I also tried strtotime.

Can someone point out what I am missing?

+6
source share
7 answers

The time () function has no parameters, so it will give you an error.

I tried using strtotime () , which may work, but it is not. I will update my answer when I find something that works. However, firstly, time () will not work.

Edit: since Phil just beat me up to seconds:

 $date = str_replace("@ ", "", "30/05/2010 @ 09:15:15"); $date = str_replace("/", "-", $date); $formatteddate = date("Ymd H:i:s", strtotime($date)); echo $formatteddate; 

Example here: http://codepad.org/heph1PG0

+6
source

If you are using PHP 5.3, try DateTime::createFromFormat() , e.g.

 $dt = DateTime::createFromFormat('d/m/Y @ H:i:s', $date); 

If not, strtotime() may work, but you need to get rid of the @ character and change the slash to hyphens (if it's an EU / AU date), for example

 $time = strtotime(str_replace(array('@', '/'), array('', '-'), $date)); 

Edit:

To display dates in the desired format, use

 echo $dt->format('Ymd H:i:s'); // for DateTime echo date('Ymd H:i:s', $time); // for strtotime 
+4
source

Your format is a bit weird ... try date_parse_from_format.

http://www.php.net/manual/en/function.date-parse-from-format.php

 $date = "30/05/2010 @ 09:15:15"; $d = date_parse_from_format('m/d/Y @ h:i:s', $date); $formatted_date = "{$d['year']}-{$d['month']}-{$d['day']} {$d['hour']}:{$d['minute']}:{$d['second']}"; 
+1
source

You have a very odd date format, so strtotime will have problems. Instead, we will use strptime , which accepts a custom format:

 $date = "30/05/2010 @ 09:15:15"; $format = "%d/%m/%Y @ %T"; $ftime = strptime($date, $format); $timestamp = mktime( $ftime['tm_hour'], $ftime['tm_min'], $ftime['tm_sec'], // Because this is 0-11 $ftime['tm_mon'] + 1, $ftime['tm_mday'], // Because this is years since 1900 $ftime['tm_year'] + 1900 ); $formatteddate = date("Ymd H:i:s", $timestamp); echo $formatteddate; 

Result:

 2010-05-30 09:15:15 
+1
source

If you are using PHP 5.3.0 or later, you can use date_parse_from_format() to parse your custom formatted date.

If you are stuck in an old version of PHP, you have to parse it yourself. I checked that this works:

 <?php function reformatDate($date) { $matches = array(); if (!preg_match('/^(\d\d)\/(\d\d)\/(\d{4})\s*@\s*(\d\d):(\d\d):(\d\d)$/', $date, $matches)) { throw new InvalidArgumentException('Invalid date supplied: ' . $date); } $day = $matches[1]; $month = $matches[2]; $year = $matches[3]; $hour = $matches[4]; $minute = $matches[5]; $second = $matches[6]; if ($day < 1 || $day > 31 || $month < 1 || $month > 12 || $hour > 24 || $minute > 60 || $second > 60) { throw new InvalidArgumentException('Invalid date supplied: ' . $date); } return "$year-$month-$day $hour:$minute:$second"; } echo reformatDate("30/05/2010 @ 09:15:15"); ?> 
+1
source

for all of you, the datez date function will not format dates like 01/01/2045, this is something php restriction for dates having large years, i.e. 2039.

0
source

Try it like this ...

 <?php $date = date_create('01-01-2001'); echo date_format($date, 'Ymd H:i:s'); ?> 
-1
source

Source: https://habr.com/ru/post/887988/


All Articles