Solved

Format a date in PHP

Posted on 2012-04-04
10
286 Views
Last Modified: 2012-04-22
$Date = "040312"; //mmddyy

I want the date to be formatted as such: 4/3/2012, how can I do that?
0
Comment
Question by:Computer Guy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 29

Expert Comment

by:Olaf Doschke
ID: 37807879
<?php
$Date = '040312';
$DateTimeObject = DateTime::createFromFormat('mdy', $Date);
echo $DateTimeObject->format('n/j/Y');
?>

Open in new window

Bye, Olaf.
0
 
LVL 3

Author Comment

by:Computer Guy
ID: 37807975
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
 
LVL 29

Accepted Solution

by:
Olaf Doschke earned 250 total points
ID: 37808221
What version of php are you using?

DateTime::createFromFormat()  exists since version 5.3.0

Bye, Olaf.
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 110

Expert Comment

by:Ray Paseur
ID: 37808822
0
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 37808833
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
 
LVL 9

Expert Comment

by:rinfo
ID: 37810427
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
 
LVL 29

Expert Comment

by:Olaf Doschke
ID: 37846023
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
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 37860179
This worked when I tested it: ID: 37808833.  Not particularly worried about the points, just keeping an accurate answer set in the PAQ.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question