-1

By default when Laravel creates a post it stores the date and time stamp as: 2019-01-31 23:32:06

How can I show it in the view as - Jan 31st, 2018 11:32pm?

user9465677
  • 437
  • 4
  • 21

3 Answers3

0

U can use Php Carbon in modifying date. eg.

$dt = Carbon::createFromFormat('Y-m-d H:i:s.u', '2019-02-01 03:45:27.612584');

// $dt->toAtomString() is the same as $dt->format(DateTime::ATOM);
echo $dt->toAtomString();           // 2019-02-01T03:45:27+00:00
echo $dt->toCookieString();         // Friday, 01-Feb-2019 03:45:27 UTC

echo $dt->toIso8601String();        // 2019-02-01T03:45:27+00:00
// Be aware we chose to use the full-extended format of the ISO 8601 norm
// Natively, DateTime::ISO8601 format is not compatible with ISO-8601 as it
// is explained here in the PHP documentation:
// https://php.net/manual/class.datetime.php#datetime.constants.iso8601
// We consider it as a PHP mistake and chose not to provide method for this
// format, but you still can use it this way:
echo $dt->format(DateTime::ISO8601); // 2019-02-01T03:45:27+0000

echo $dt->toISOString();            // 2019-02-01T03:45:27.612584Z
echo $dt->toJSON();                 // 2019-02-01T03:45:27.612584Z

echo $dt->toIso8601ZuluString();    // 2019-02-01T03:45:27Z
echo $dt->toDateTimeLocalString();  // 2019-02-01T03:45:27
echo $dt->toRfc822String();         // Fri, 01 Feb 19 03:45:27 +0000
echo $dt->toRfc850String();         // Friday, 01-Feb-19 03:45:27 UTC
echo $dt->toRfc1036String();        // Fri, 01 Feb 19 03:45:27 +0000
echo $dt->toRfc1123String();        // Fri, 01 Feb 2019 03:45:27 +0000
echo $dt->toRfc2822String();        // Fri, 01 Feb 2019 03:45:27 +0000
echo $dt->toRfc3339String();        // 2019-02-01T03:45:27+00:00
echo $dt->toRfc7231String();        // Fri, 01 Feb 2019 03:45:27 GMT
echo $dt->toRssString();            // Fri, 01 Feb 2019 03:45:27 +0000
echo $dt->toW3cString();            // 2019-02-01T03:45:27+00:00

Kindly read more about Php Carbon

MONSTEEEER
  • 544
  • 6
  • 23
0

You can use Carbon in laravel.

First, use carbon:

use Carbon\Carbon;

Then use the format you want, in your case, it would be like this:

public function test()
{
    $dt = User::find(1);
    return $dt->created_at->format('M jS\\, Y h:i:s A'); //Dec 1st, 1975 02:15:16 PM
}

You can also use it in your view, just use carbon at the top of your file:

<?php use Carbon\Carbon;?>

then change the format in your display like {{$dt->created_at->format('M jS\\, Y h:i:s A')}}

Kapitan Teemo
  • 2,144
  • 1
  • 11
  • 25
0

Your input is 2018-01-31 23:32:06.

Your expected output is Jan 31st, 2018 11:32pm.

I have created a function:

function convertToPreferedDateFormat($dateString='')
  {
            $toTimeString = strtotime($dateString);

            $yourExpectedFormat = 'M dS, Y h:ia';

           // $prettyBeautifullWay = 'l jS \of F Y h:i:s A';

            return date($yourExpectedFormat, $toTimeString);
  }

Then now testing

$timeString = '2018-01-31 23:32:06';

  $expectedOutput = 'Jan 31st, 2018 11:32pm';

echo convertToPreferedDateFormat($timeString);
echo "<br>";
echo $expectedOutput;
halfer
  • 19,824
  • 17
  • 99
  • 186
ManojKiran A
  • 5,896
  • 4
  • 30
  • 43