Working with DateTime and format in laravel

13. October 2016 Intermediate, Laravel 0

Hello everyone, Welcome back ! Here am with another interesting topic, it is about DateTime and changing the format of it in our applications.

We come across many situations where we need to use some date and also format it, and sometimes also to change the language of it.

Here we display the date in Arabic language.

Working Demo      Project on Github

The simple approach we follow is, initially we add an item, get its actual date using php DateTime function, then convert its month to Arabic and finally store it in the database.

Adding data :

So a simple form is needed, to store an item along with created date, the form is as below,

Storing data :

To store this data we create a table, in migrations up function,

and a model file for it,

Working with DateTime :

So now when the form is submitted. it is routed to,

Working Demo      Project on Github

DateTime with format options :

Here we formatted the date with 'j M Y H:i:s A e' so it gives

  • j – Day of the month without leading zeros ( 1 to 31 )
  • M – A short textual representation of a month, three letters ( Jan through Dec )
  • Y – A full numeric representation of a year, 4 digits ( Examples: 1999 or 2003 )
  • H:i:s – Hours, minutes and seconds
  • A – AM / PM
  • e – Time zone ( default UTC )

There are also different formatting options available for, day, week, month, year, time, timezone. The following images show the available formats.

Day :

Working with DateTime –

Week :

Working with DateTime –

Month :

Working with DateTime –

Year :

Working with DateTime –

Time :

Working with DateTime –

Timezone :

Working with DateTime –

Full Date/Time :

Working with DateTime –

Source link :


Showing the data :

After the data is saved, we get all the data from the database,

and will show it,

Working with DateTime –


Working Demo      Project on Github

Leave a Reply