Prosp
Prosp

Reputation: 151

php convert date with two digit year

i retrieve a date from xml file with a two digit year like 'dd/mm/yy'. how can i change it to 'yyyymmdd' format. thanks for your help

This is what i've tried, but i'm getting this date 01/01/1970:

    $date =  str_replace('/', '-', $date_reception);

    $timestamp = strptime($date);

    $this->date_reception = date("Ymd", $timestamp);

Upvotes: 2

Views: 4390

Answers (2)

olibiaz
olibiaz

Reputation: 2595

You can use DateTime::createFromFormat() function

http://php.net/manual/fr/datetime.createfromformat.php

$input = '01/05/16';
$dateTime = \DateTime::createFromFormat('d/m/y', $input);
echo $dateTime->format('Ymd'); // will output 20160501

// or in one line
$date = \DateTime::createFromFormat('d/m/y', $input)->format('Ymd');

Upvotes: 2

RiggsFolly
RiggsFolly

Reputation: 94662

Simply convert your date to a timestamp using strtotime()

$date =  str_replace('/', '-', $date_reception);
$this->date_reception = date('Ymd', strtotime($date));

Upvotes: 0

Related Questions