Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 299
  • Last Modified:

Format a date in PHP

$Date = "040312"; //mmddyy

I want the date to be formatted as such: 4/3/2012, how can I do that?
0
Computer Guy
Asked:
Computer Guy
2 Solutions
 
Olaf DoschkeSoftware DeveloperCommented:
<?php
$Date = '040312';
$DateTimeObject = DateTime::createFromFormat('mdy', $Date);
echo $DateTimeObject->format('n/j/Y');
?>

Open in new window

Bye, Olaf.
0
 
Computer GuyAuthor Commented:
Hi,

Thanks, but I got this error:

Fatal error: Call to undefined method DateTime::createFromFormat() in /home/test/public_html/datetest.php on line 3
0
 
Olaf DoschkeSoftware DeveloperCommented:
What version of php are you using?

DateTime::createFromFormat()  exists since version 5.3.0

Bye, Olaf.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Ray PaseurCommented:
0
 
Ray PaseurCommented:
See http://www.laprbass.com/RAY_temp_audiotech.php
<?php // RAY_temp_audiotech.php
error_reporting(E_ALL);
echo "<pre>";

date_default_timezone_set('America/Chicago');

// NEVER STORE A DATE IN THIS FORMAT - USE ISO-8601 INSTEAD
$date = '040312';

// REFORMAT THE DATE
$new
= substr($date,0,2)
. '/'
. substr($date,2,2)
. '/'
. substr($date,4,2)
;
echo $new;
echo PHP_EOL;

// CONVERT TO ISO-8601
$iso = date('c', strtotime($new));
echo $iso;
echo PHP_EOL;

// CONVERT TO ANOTHER FORMAT
$alt = date('n/j/Y', strtotime($new));
echo $alt;
echo PHP_EOL;

Open in new window

0
 
rinfoCommented:
This can be done using following code.
$Date = "040312";
$m  =  substr($date,0,2);
$d =   substr($date,2,2);
$y =    substr($date,4,2);
$uDate  = mktime(0, 0, 0, $m, $d, $y);
$fdate   =  date("m/d/y", $udate);

However end point is what Ray has to say . Stick to convention always.
0
 
Olaf DoschkeSoftware DeveloperCommented:
I asume audiotech520 has updated PHP so my first answer works, but then that answer should be marked, not the (indirect) advice to update PHP.

It would be fair and I would not mind, if points would be shared on several answers, which also work with older PHP versions.

Bye, Olaf.
0
 
Ray PaseurCommented:
This worked when I tested it: ID: 37808833.  Not particularly worried about the points, just keeping an accurate answer set in the PAQ.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now