Problem with current time in PHP and MySQL

2

Since I have several date columns in the table and I can not use more than one column CURRENT_TIMESTAMP , through a function that I created I get the current date. The function is as follows:

function dame_fecha_actual() {
    $hoy = getdate();
    $meses = ['enero', 'febrero', 'marzo', 'abril', 'mayo', 'junio','julio','agosto','septiembre','octubre','noviembre','diciembre'];

    $minuto = $hoy['minutes'];
    $hora = $hoy['hours'];
    $day = $hoy['mday'];
    $mes = $hoy['mon'];
    $segundos = $hoy['seconds'];

    if ($hoy['hours'] <= 9) {
        $hora = "0" . $hoy['hours'];
    }

    if ($hoy['minutes'] <= 9) {
        $minuto = "0" . $hoy['minutes'];
    }

    if ($hoy['mon'] <= 9) {
        $mes = "0" . $hoy['mon'];
    }

    if ($hoy['mday'] <= 9) {
        $day = "0" . $hoy['mday'];
    }

    if ($hoy['seconds'] <= 9) {
        $segundos = "0" . $hoy['seconds'];
    }

    $fecha_total_actual = $hoy['year'] . "-" . $mes . "-" . $day . " " . $hora . ":" . $minuto . ":" . $segundos;

    return $fecha_total_actual; }

I realized that the local does not happen to me, but on the web at the time of entering the time to the database, I have two hours left at the time it really is here (I live in Spain).

From what is commented on the PHP page, the function getdate () should give me the current local time if I do not add $timestamp .

    
asked by JetLagFox 06.07.2017 в 05:40
source

1 answer

3

Solution - PHP

Although I try to reproduce the error, it has been in vain, since in both php-cli and apache, they throw the same time, there is something you could take into account:

There is a function in PHP, called date_default_timezone_set () , this allows "configure" a time zone, for your program.

How does it work?

Simply add the function, indicating the time zone to use:

<?php

date_default_timezone_set("America/Los_Angeles");

function dame_fecha_actual() {
...
}

What we tell the program, is that it uses by default the time zone of "Los Angeles".

Which instead of throwing:

  

2017-07-05 23:20:45

Arrojara:

  

2017-07-05 21:20:53

You can find in the PHP documentation a list of valid time zones.

Solution - Apache

It is a possibility that you have unconfigured the time on the Apache server. The solution for this type of problem is very similar to the previous one:

  • We open the PHP.INI file (/ etc / php5 / apache2 / in GNU / Linux).
  • You search for the line date.timezone .
  • You add the time zone, according to the PHP documentation.
  • It must pass from:

    date.timezone =
    

    To stay, (for example):

    date.timezone = "America/Bogota"
    

    And it would be enough to restart the Apache so that the changes take effect.

        
    answered by 06.07.2017 / 06:21
    source