无法将日期转换为所需的格式,如(2015年10月21日星期三下午13:04)[重复]
This question already has an answer here:
- Convert one date format into another in PHP 15 answers
i am trying to convert current date into the following format:
Example: Wed, Oct 21, 2015 at 13:04 PM
Just like gmail.
i have searched a lot on internet but nothing helped. I have also studied the following link, http://php.net/manual/en/datetime.formats.date.php
but still unable to sort out my problem.
Please help me to sort out this issue.
</div>
此问题已经存在 这里有一个答案: p>
-
在PHP中将一种日期格式转换为另一种格式
15 answers
li>
ul>
div>
我正在尝试将当前日期转换为以下格式: p>
示例: 星期三,2015年10月21日下午13:04 strong> p>
就像gmail一样。 p>
我在互联网上搜索了很多,但没有任何帮助。 我还研究了以下链接, http://php.net/manual /en/datetime.formats.date.php p>
但仍然无法解决我的问题。 p>
请帮我排序 出了这个问题。 p> div>
try this:
<?php
date_default_timezone_set('America/Los_Angeles');
$time = strtotime(Date('Y-m-d H:i:s'));
$month=date("F",$time);
$year=date("Y",$time);
$day=date("d",$time);
$hour=date("H",$time);
$minute=date("i",$time);
$x=date("A",$time);
$final=substr(date("l"),0,3).", ".substr($month,0,3)." ".$day.", ".$year." at ".$hour.":".$minute." ".$x;
echo $final;
?>
you'll get the desired output. :)
You can use the date and time formatters. The lesser known are D for abbreviated day of the week, M for abbreviated month, and j for day number without leading 0. For the time format you can use g, which is 12 hour time without leading zero and A for uppercase Ante meridiem and Post meridiem indication. The various letters and their meaning are listed on the documentation page of the date()
function.
You'll either need to escape the characters of at
by adding a backslash for each of them. Or you can choose to format in two steps:
echo date( 'D, M j, Y') . ' at ' . date('g:i A');
For a specific timestamp:
$timestamp = time();
echo date( 'D, M j, Y', $timestamp) . ' at ' . date('g:i A', $timestamp);
Using escaping and poored into a function:
function gmail_date($timestamp = null) {
// Allow passing no time, just like date() does.
if ($timestamp === null) $timestamp = time();
return date( 'D, M j, Y \a\t g:i A', $timestamp);
}
echo gmail_date(time());
Look at PHP Date function for formatting tips.
echo date('D, M j, Y \a\t g:i A', strtotime('+2 weeks 1 hour'));
Returns Wed, Nov 4, 2015 at 5:14 PM which looks pretty legit.