display date format [closed]
I want to display 2-March-2011 is like '02-March-2011'.
<?php
// Assuming today is March 10th, 2001, 5:16:18 pm, and that we are in the
// Mountain Standard Time (MST) Time Zone
$today = date("j-F-Y"); // 10-March-2001
$today = date("F j, Y, g:i a"); // March 10, 2001, 5:16 pm
$today = date("m.d.y"); // 03.10.01
$today = date("j, n, Y"); // 10, 3, 2001
$today = date("Ymd"); // 20010310
$today = date('h-i-s, j-m-y, it is w Day'); // 05-16-18, 10-03-01, 1631 1618 6 Satpm01
$today = date('\i\t \i\s \t\h\e jS \d\a\y.'); // it is the 10th day.
$today = date("D M j G:i:s T Y"); // Sat Mar 10 17:16:18 MST 2001
$today = date('H:m:s \m \i\s\ \m\o\n\t\h'); // 17:03:18 m is month
$today = date("H:i:s"); // 17:16:18
?>
Refer this link also....
http://php.net/manual/en/function.date.php
Just replace the hardcoded date with your value
$timestamp = strtotime('2-March-2011');
$newDate = date('d-F-Y', $timestamp);
echo $newDate; //outputs 02-March-2011
You could use regex, string splitting, sprintf()
, etc, but I'd rather use the native DateTime object.
$str = '2-March-2011';
$date = DateTime::createFromFormat('j-F-Y', $str);
var_dump($date->format('d-F-Y')); // string(13) "02-March-2011"
See it.
Convert the input string to a timestamp, then format that as desired. See http://php.net/manual/en/function.date.php for formatting options.
$input = '2-March-2011';
$time = strtotime($input);
$output = date('d-F-Y',$time);
Short version:
echo date('d-F-Y',strtotime('2-March-2011'));
A quick hop over to the documentation reveals that everyone else was correct when they said this:
echo date('d-F-Y', strtotime('2-March-2011'));
If you have the date as a Unix timestamp (eg: $date = time();
), then you can just use the inbuilt date()
function:
echo date('j-F-Y');
Otherwise, you can look at using strtotime()
(http://www.php.net/manual/en/function.strtotime.php) to get it into Unix time, or alternatively use a more object-oriented approach as alex has shown.
Code Sample : echo date('d-F-Y',strtotime('2011-03-02'));
you can format you date in many ways here you can find all the formats date format
精彩评论