
Working with dates and times in any application is essential, we can obtain them with the functions provided by the language and do the operations we need with each of them.
But there are occasions that these values do not come according to the time zone in which we are or are set according to the values of the server where we are running our application.
get age with birth date in PHP
If we are working with PHP, we have a simple function that allows us to solve this problem, let's see.
PHP code
If we have worked with PHP we know that we have a fairly simple function to obtain both date and time, and it is the date () function , that by sending different parameters we can obtain these values formatted in different ways.
Generate random passwords in PHP
The problem is when we use it without indicating the time zone, so it is likely that we get the date correctly but most likely we will not run with the same fate with the time. Let's look at a code using only date ():
Get current PHP date and timeHere we use date () to return the month, day and year, as well as the hour with the minutes and the anti meridian. Let's see what returns us:Current Date and Time
As we see the date is correct but the time disagrees with our current time, to solve this problem we will use the date_default_timezone_set () function, which receives as a parameter the time zone as a string, to consult all the options of this function we can enter here.
Get current PHP date and timeWe have sent to the function the time zone of Madrid that is where we are, if we run our script again we will get the current date and time correctly, let's see.Current Date and Time
Once this is done, we can see how we can obtain the correct date and time according to our time zone, we should only consult the list of options that have the function and use it according to where we are to obtain the correct result that we can use to the operations we need in our application.
Articles