Laravel: Changing Date Format To Timestamp

Creating a website using the Laravel framework is very helpful with the extensions that have been provided. One very helpful extension is Carbon which can be used to manipulate the date and time. Converting a date format to a timestamp using Carbon is very easy to do.

Let’s say we have a date with the format Y-m-d and want to convert it to a timestamp then it can be done using the Carbon.

Date From String

For example, suppose we have date data in a string with the format Y-m-d: 2022-07-14. We can convert it to a timestamp in the following way:

$date = '2022-07-14';
$timestamp = Carbon::make($date)->timestamp;

The date that has been converted into a timestamp can be used to fill in the fields created_at or updated_at which are automatically generated when running the migration.

Date From Query POST Request

Sending date data via POST Request is very common. One of them is to give the user the flexibility to set the date the transaction is being made. For example, when filling out a purchase transaction, it is permitted to enter previous purchase data or a backdate.

Example: trx_date is used as a key to mark the transaction date sent via POST Request. Then the way to change the transaction date to a timestamp is as follows.

$date = $request->trx_date;
$timestamp = Carbon::make($date)->timestamp;

Contact

Noborejo Argomulyo, Salatiga 50736
Monday, Tuesday, Wednesday, Thursday, Saturday, Sunday09:00 – 17:00

Connect

Subscribe

Join our email list to receive the latest updates.

%d bloggers like this: