1

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.

3 Answers3

2

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());
GolezTrol
  • 114,394
  • 18
  • 182
  • 210
1

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. :)

Amrinder Singh
  • 5,300
  • 12
  • 46
  • 88
  • 1
    wow! yes it works perfectly, thanks so much:) –  Oct 21 '15 at 20:18
  • Even though the answer produces the results, the code is overly complicated and hard to maintain. See my solution and @GolezTrol 's. – jpaljasma Oct 23 '15 at 02:01
1

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.

jpaljasma
  • 1,612
  • 16
  • 21