Write the date in PHP

One of the most common things in programs is the management of dates. It is very normal that we have to get the date in PHP for different needs, whether queries to the database, showing information to the user,…

In this small article we are going to see how easy it is to write the date in PHP.

Set geographic area

Firstly to be able to use the date in PHP we have to keep in mind that there are variants from different regions of the planet.

To express dates, therefore, we must use the function date_default_timezone_set to detail the geographical area from which we will get the date to use.

//we define the geographic area of ​​which we want the date
date_default_timezone_set("America/Lima");//Peru time zone

Get the date in PHP

The next thing we must do is take into account the use of the date function with the correct parameters in this case to obtain the date we need the day, month, and year to obtain this data we use as parameter:

  • «d», for day.
  • «m», for month.
  • «And», for the year.

We are left with a code as seen below:

//we define the geographic area of ​​which we want the date
date_default_timezone_set("America/Lima");
//extract the date using the date function 
// d: day
// m: month
// Y: year in four digits
echo date("d/m/Y"); 
exit;//we end the exit

With this simple code we have already managed to write the date in PHP.