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;
return $date;

2020-09-01 15:03:59

toDateString() :

It provides on the Date from your DateTime string .

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


toTimeString() :

It provides the Time from your DateTime string .

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


toDayDateTimeString() :

It provides date and time as shown below .

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

Tue, Sep 1, 2020 3:03 PM

Post a Comment

Post a Comment (0)