Format date GMT Madrid PHP

0

When entering the variable to obtain the date, I am looking at the log that is left to me by two hours before the current one in Spain.

$nowtimename = gmdate('Y-m-d h:i:s \G\M\T', time());

And I need it to be the one we use right now in Spain, not having to think that it is always two hours behind, I guess adding a variable that adds two could be enough, but if there is any way to do it in gmdate better.

    
asked by Alberto Cepero de Andrés 29.08.2017 в 09:51
source

2 answers

3

According to the PHP online manual ( link ) with the gmdate() function you can not change the Time zone:

  

Identical to the date () function except that the time returned is   Greenwich Mean Time (GMT).

With the function date() it can be done in the following way:

date_default_timezone_set('Europe/Madrid');
$nowtimename = date('Y-m-d h:i:s \G\M\T', time());
echo $nowtimename;




I edit my answer because by looking a little deeper I have seen that you can do the following if you need to do it with the function gmdate() :

$timezone  = 1;  // Madrid: GTM + 1
echo gmdate("Y-m-d h:i:s \G\M\T", time() + 3600*($timezone+date("I"))); 

Personally, I prefer the first option but it will depend on your needs.

    
answered by 29.08.2017 / 10:08
source
0

You have to specify your time zone before generating the date.

date_default_timezone_set('Europe/Madrid');
    
answered by 29.08.2017 в 09:53