Help with php date formatting

Jonathan Greenberg
Jonathan Greenberg used Ask the Experts™
on
I want a string of "10/01/2011" to be formatted as Oct 1, 2011 with php. How do I do this? Using strtotime()? Or srtftime()? Or date()?

I've done this before, but I can't quite seem to remember how. Thanks.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Fixer of Problems
Most Valuable Expert 2014
Commented:
This was from another question.  Lots of variations on this page: http://www.php.net/manual/en/function.date.php and here http://www.php.net/manual/en/function.strtotime.php .  You may need to use explode() first because "10/01/2011" in some locales is Jan 10, 2011.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>PHP Date Time</title>
</head>
<body>
<h1>PHP Date Time</h1>
<?php 
$unixtime = 1298073600 ;
$realtime = strftime("%b %d %Y",$unixtime);
echo $realtime."<br>";
$today = date("Y-m-d H:i:s", $unixtime);
echo $today."<br>";


$str = 'February 19, 2011';

// previous to PHP 5.1.0 you would compare with -1, instead of false
if (($timestamp = strtotime($str)) === false) {
    echo "<br>The string ($str) is bogus";
} else {
    echo "<br>$str == " . date('l dS \o\f F Y h:i:s A', $timestamp);
		echo "<br>".$timestamp."  ".$unixtime;
}
$str = 'February 18, 2011';

// previous to PHP 5.1.0 you would compare with -1, instead of false
if (($timestamp = strtotime($str)) === false) {
    echo "<br>The string ($str) is bogus";
} else {
    echo "<br>$str == " . date('l dS \o\f F Y h:i:s A', $timestamp);
		echo "<br>".$timestamp."  ".$unixtime;
}
 ?>
</body>
</html>

Open in new window

Sandeep KothariProject Lead
Commented:
$dt = "10/01/2011";
date('M d, Y', strtotime($dt));
Juan OcasioApplication Developer
Commented:
$date = strtotime('10/01/2011');
$newdate= date("M d, Y", $date);

Author

Commented:
kshna and jocasio123, those work except the 'd' in the date() function needs to be a 'j' for the day to display without a leading zero (1 to 31).

Dave, thanks you for all your examples and for pointing out the potential locale issue.
Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
You're welcome, glad to help.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial