OllyBarca
OllyBarca

Reputation: 1531

Php - How to reorder a date

I have a date which is formatted simply as 19830210 (YYYYDDMM).

How would I reorder it to 02101983 (DDMMYYYY) using php?

I simply want to rearrange the digits in the date.

Upvotes: 3

Views: 1821

Answers (4)

Zahidul Hossein Ripon
Zahidul Hossein Ripon

Reputation: 672

$str='19830210'; //(YYYYDDMM).
echo $str."<br/>";
$yy=substr($str,0,4);
$mm=substr($str,4,2);
$dd=substr($str,6,2);
$reorder=$dd.$mm.$yy;
echo $reorder;   //(DDMMYYYY).

Upvotes: 0

Ashkan Arefi
Ashkan Arefi

Reputation: 655

use substr() function:

<?php
$date1 = 'YYYYDDMM';
$date2 = substr($date1 , 4 , 4).substr($date1 , 0 , 4);
echo $date2;
?>

Upvotes: 0

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100195

use createFromFormat(), do:

$date = "19830210";
$newFormat = DateTime::createFromFormat('Ydm', $date);
echo $newFormat->format('dmY'); //will give you 02101983 

Upvotes: 8

Veerendra
Veerendra

Reputation: 2622

 echo $output=date('dmY',strtotime('02101983'));

For future use with date format use below link for reference http://php.net/manual/en/function.date.php

Upvotes: 1

Related Questions