How do I know the time and date of a location with PHP?

Asked

Viewed 126 times

5

I intend to store in a variable the current time of Portugal and in another variable the current date of Portugal.

How can I do?

2 answers

3


You can do it like this:

date_default_timezone_set('Europe/Lisbon');

$dia = date('Y-m-d'); // 2016-12-24
$hora = date('G:i'); // 16:29

DEMONSTRATION

Note that in the demonstration, the time is not right, but must be because of the ideone platform.

1

Configure the date_default_timezone_set for Europe/Lisbon and see working here in ideone:

date_default_timezone_set('Europe/Lisbon');

$date = time();
$date = date('Y-m-d', $date);
$hour = date('H:i:s', $date);
echo "Data: ".$date."\n";
echo "Hora: ".$hour;

You can check the Time Zones support list here in the documentation of PHP.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.