How to show local time with PHP?

2

Try the following script:

<?php
$hora = date("G:a");
echo $hora;
?>

Select the G format to print the time without the initial 0, but print me 14: pm and on my computer it's 11: am, how do I synchronize php with my local time?

    
asked by gburu98 09.06.2016 в 16:26
source

3 answers

5

You must add your time zone to show the date (or before), there are several ways to do it, one of them is with DateTime :

$hora = new DateTime("now", new DateTimeZone('America/New York'));
echo $hora->format('G');

You could also use date_default_timezone_set :

date_default_timezone_set("America/New York");
echo date('G');
    
answered by 09.06.2016 / 16:33
source
0

You can try the following;

<?php 
$fecha = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru'));
echo $fecha->format('Y-m-d H:i:sP') . "\n";
$fecha->setTimezone(new DateTimeZone('Pacific/Chatham'));
echo $fecha->format('Y-m-d H:i:sP') . "\n";
?>

Information that was obtained from the official PHP documentation link

    
answered by 09.06.2016 в 16:34
0

If you only want to show the time of your server, you can do it in the following way:

<?php
  echo date('h:i:s A');
?>
    
answered by 09.06.2016 в 16:36