无法将日期转换为所需的格式,如(2015年10月21日星期三下午13:04)[重复]

无法将日期转换为所需的格式,如(2015年10月21日星期三下午13:04)[重复]

问题描述:

This question already has an answer here:

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>

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.