PHP Laravel Tip: How to Extract Date, Time, and Day Easily

 How to get only Date , Time , Day , DateTime From DateTime Variable in Laravel PHP

Get only Date , Time , Day from DateTime - PHP Laravel

In this article we will learn some basics PHP functions to convert our DateTime values into a readable form .

Let's say we have a variable with the following DateTime value 

$date = 2020-09-01T15:03:59.000000Z

To use the methods in your controller ,  use the Carbon class as shown below .

use Carbon\Carbon;

toDateTimeString() :

It converts your existing DateTime format to readable Date and Time format .

$date = 2020-09-01T15:03:59.000000Z;
$date->toDateTimeString();
return $date;

//output
2020-09-01 15:03:59

toDateString() :

It provides on the Date from your DateTime string .

$date = 2020-09-01T15:03:59.000000Z;
$date->toDateString();
return $date;

//output
2020-09-01

toTimeString() :

It provides the Time from your DateTime string .

$date = 2020-09-01T15:03:59.000000Z;
$date->toTimeString();
return $date;

//output
15:03:59

toDayDateTimeString() :

It provides date and time as shown below .

$date = 2020-09-01T15:03:59.000000Z;
$date->toDayDateTimeString();
return $date;

//output
Tue, Sep 1, 2020 3:03 PM



Previous Post Next Post

Contact Form