Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining the user's time and date in PHP

Tags:

php

datetime

I want to be able to get the time of the person using the PHP script.

I don't know what time zone they will be in, etc.

Is there not a way of just saying 'get the time of the user' rather than setting timezones?

like image 389
daihovey Avatar asked Dec 15 '10 05:12

daihovey


People also ask

Which function is used to find the current date and time PHP?

?> Note that the PHP date() function will return the current date/time of the server!

How can you generate current date and time in PHP?

Answer: Use the PHP date() Function You can simply use the PHP date() function to get the current data and time in various format, for example, date('d-m-y h:i:s') , date('d/m/y H:i:s') , and so on.

How can I get current date in dd mm yyyy format in PHP?

In the below example, we have date 2019-09-15 in YYYY-MM-DD format, and we will convert this to 15-09-2019 in DD-MM-YYYY format. $orgDate = "2019-09-15"; $newDate = date("d-m-Y", strtotime($orgDate)); echo "New date format is: ".


1 Answers

No. There are only three ways to get the time zone of the user:

  1. Ask the user

  2. Use JavaScript to check the time client-side and send it to your server with an AJAX request, a cookie, etc. -- only works if JS is enabled and doesn't work for the first request

  3. Use an IP-to-location database to locate the user and determine their time zone from the location -- more than 5% of the time with any commercial database, your location at the city level will be wrong

like image 81
Dan Grossman Avatar answered Sep 30 '22 15:09

Dan Grossman