getdate () can be configured for the time of my pc?

1

I am trying to implement a clock in php for my project, a clock that contains the date and time of my computer, until now I know something about how the function getdate () of php works and this is what I have:

<?php
  $hoy = getdate();           //contiene todos los datos de fecha y hora

  $dia = $hoy["wday"];        //obtengo número del día actual (0 a 6)
  $dias = ["Domingo","Lunes","Martes","Miércoles","Jueves","Viernes","Sábado"];
  $dia = $dias[$dia];         //capturo el nombre del día

  $ndia = $hoy["mday"];       //el número del día (1 a 31)
  $mes = $hoy["mon"];         //el número del mes (1 a 12)
  $meses = ["Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre",];
  $mes = $meses[$mes-1];      //capturo el nombre del mes

  $año = $hoy["year"];        //el año (4 dígitos)
  $hora = $hoy["hours"];      //la hora  (esta necesito que sea la de mi pc)
  $minuto = $hoy["minutes"];  //minutos  (estos sí son los de mi pc)

  $ceroh = "";  //cero extra por estética de la hora
  if($hora < 10){
    $ceroh = "0";
  }else{
    $ceroh = "";
  }
  $cerom = ""; //cero extra por estética de los minutos
  if($minuto < 10){
    $cerom = "0";
  }else{
    $cerom = "";
  }

    print_r("<a href='#' id='fechayhora'>"
      .$dia." "
      .$ndia." de "
      .$mes." del "
      .$año.", "
      .$ceroh.$hora.":"
      .$cerom.$minuto
      ." hrs</a>");

?>

The output is as follows: Wednesday November 2, 2016, 2:59 hrs

but the day and time are wrong because at this time is Tuesday November 1, 2016, 22:59 hrs on my pc and that is the time I want to show, you can see that is 4 hours ahead of time mine, that's why it also shows tomorrow.

Is there any way to take 4 hours of data that gets delivered to me ()?

EDIT

here says that the getdate () form is:

array getdate ([int $ timestamp = time ()])

And it returns an associative array that contains the information of the timestamp date, or the current local moment if timestamp is not given.

And I'm not passing that timestamp, so you should give me the local time, which is my pc or am I wrong?

EDIT 2

When I refer to getting the time of my pc, it is because in this case my pc is the server, the objective of the question is to modify the default time that the getdate () function brings, in this case, to remove it 4 hours . No reference is made to the time use of a city, although this is the formal way of working with dates and times on a server.

    
asked by Roberto Sepúlveda Bravo 02.11.2016 в 03:02
source

3 answers

0

I have found the solution, before declaring the variable $hoy of the code, I add these lines:

$timestamp = time() - (60*60*4);
$hoy = getdate($timestamp);

Basically I am subtracting 60 seconds for 60 minutes for 4 hours, which translates to 14400 seconds, that is to say the 4 hours that I needed to subtract from that function.

The usefulness of this is that we will have a "manual" clock, so to speak, modifiable only by us and not by the time zone, an option for countries like Chile, where governments play a lot with the time. The final code would look like this:

<?php
  $timestamp = time() - (60*60*4);  //capturamos el tiempo -4 horas
  $hoy = getdate($timestamp);       //capturamos los datos de fecha y hora de acuerdo al timestamp recién hecho

  $dia = $hoy["wday"];        //obtengo número del día actual (0 a 6)
  $dias = ["Domingo","Lunes","Martes","Miércoles","Jueves","Viernes","Sábado"];
  $dia = $dias[$dia];         //capturo el nombre del día

  $ndia = $hoy["mday"];       //el número del día (1 a 31)
  $mes = $hoy["mon"];         //el número del mes (1 a 12)
  $meses = ["Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre",];
  $mes = $meses[$mes-1];      //capturo el nombre del mes

  $año = $hoy["year"];        //el año (4 dígitos)
  $hora = $hoy["hours"];      //la hora  (esta necesito que sea la de mi pc)
  $minuto = $hoy["minutes"];  //minutos  (estos sí son los de mi pc)

  $ceroh = "";  //cero extra por estética de la hora
  if($hora < 10){
    $ceroh = "0";
  }else{
    $ceroh = "";
  }
  $cerom = ""; //cero extra por estética de los minutos
  if($minuto < 10){
    $cerom = "0";
  }else{
    $cerom = "";
  }

    print_r("<a href='#' id='fechayhora'>"
      .$dia." "
      .$ndia." de "
      .$mes." del "
      .$año.", "
      .$ceroh.$hora.":"
      .$cerom.$minuto
      ." hrs</a>");

?>

If you want to change the time, you simply have to replace that "4" with a variable and then add or subtract some value brought from a database.

EDIT

This solution is for when we want to change the time over which the time () function already brings, which was what I was initially looking for in my question, but as @rfrp said in its answer, it is necessary to take into account the hourly use of the city where we are, that's why a formal solution would be, now for the first three lines of code:

date_default_timezone_set("tu_continente/tu_ciudad"); // ("America/Santiago") por ejemplo
$timestamp = time();
$hoy = getdate($timestamp);    //y desde aquí puedes obtener date y time según tu ciudad

But this would be sticking to the time zone as standard as many systems do, which would change our time automatically and not manually as we were looking in this case.

    
answered by 02.11.2016 / 03:39
source
0

And if you try like this:

<?php
    $dias = array ( 'Lunes', 'Martes', 'Miercoles','Jueves',
                    'Viernes', 'Sabado', 'Domingo' );
    $meses = array ( 'Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio',
            'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre' );
    $dia = intval(date('N')) - 1;
    $mes = intval(date('m')) - 1;

    echo $dias[$dia].' '.date('d').' de '.$meses[$mes].' del '.date('Y, H:i').' hrs';
?>
    
answered by 02.11.2016 в 03:26
0

While the code you did, to subtract the hours works, that solution does not scale. The problem that is happening to you is that you are getting the time in UTC. You are probably standing in a city with GMT-4 time use and that's why you have 4 hours less.

As a tip, consider adjusting the hours according to the time zone of your city / geographical area.

Out of this comment in the PHP documentation :

<?php
date_default_timezone_set("UTC");
echo "UTC:".time();
echo "<br>";

date_default_timezone_set("Europe/Helsinki");
echo "Europe/Helsinki:".time();
echo "<br>";
?> 

Notice that what they do in the routine is set the time zone or city to show the time.

Eye, you also have a problem in the approach to your question. You say that you want it to show the time of your computer, but in reality it is showing you the time of the server that in your case coincides with the server is YOUR PC. If you would like to take time out of YOUR PC, you would have to do some javascript routine (which is executed locally).

    
answered by 02.11.2016 в 05:00