PHP Timestamp Changes before midnight to next day

EdgarAlexPoe

I've tried different ways to get my timestamp to change at midnight to the next day, but I haven't had any luck. I am using Wordpress, and changed the default location to Mountain Time. I figured that these two would communicate together but I haven't had any luck.

<?php current_time(timestamp);   echo date('l, F jS Y'); ?>

<?php ini_set('date.timezone', 'America/Denver');
echo date('l, F jS Y'); ?>

The first one I am trying now, and the second snippet is one I was using prior.

Its hard to debug this, since I have to wait until the night to see if the date changes early to the next day. Not optimal. I appreciate any help on this.

David Lemon

It seems that you don't have the timezone well configured in your server. Just output the full date and time, you don't have to wait for midnight to get the difference between the right and wrong hour.

Regarding time and timezone in php you have to know two facts:

  • timestamps are always GMT, NOT depending on the timezone
  • date output is a timestamp formatted, depending on the timezone

Knowing this you can easily debug your program.

<?php 

echo date('r').PHP_EOL; 
// timezone not configured
// Wed, 02 Jan 2019 16:11:36 +0000
echo date('U').PHP_EOL; 
// 1546445496
ini_set('date.timezone', 'America/Denver');
echo date('r').PHP_EOL;
// Wed, 02 Jan 2019 09:11:36 -0700
echo date('U').PHP_EOL; 
// 1546445496 , same as above

From this point you have many solutions, I recommend the following two:

  • Put this in your php.ini file:

     date.timezone="America/Denver"
    
  • If you cannot edit the php.ini, use the DateTime object to convert it manually.

     $d = new DateTime();
     $d->setTimezone(new DateTimeZone('America/Denver'));
     echo $d->format('r').PHP_EOL;
     // Wed, 02 Jan 2019 09:11:36 -0700
    

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

PHP Date (day) changes before midnight

Php get timestamp of previous day midnight

Echo Date - Date turning to the next day before midnight

PHP - convert midnight date into timestamp

Test if a Time is the next day, not including midnight

I want to get the timestamp of next day 12PM in PHP

Count distinct sql and break by day with timestamp over midnight

Pandas datetime column increment day when reach midnight timestamp

How to find events the next day and after midnight in MySQL

Sql split entries into two entries if EndDateTime is the next day(after midnight)

Timestamp1 is next day of timestamp2

Getting a timestamp for today at midnight?

PHP DatePeriod before and after midnight with time only without date

How to get the unix timestamp of the midnight (beginning of the day) for a given date and timezone using JavaScript?

how to find next of next day in php

Using next day in strtotime with PHP

How to find the difference between two times when times after midnight but still early morning are the next day

PHP generate timestamp for each day of the week

Getting day of week from timestamp PHP

MySQL - Select where timestamp is midnight

Time difference with day jump (midnight)

How to find the first day of NEXT month - PHP

PHP DateTime first day of next year

Issue with formula and before/after midnight

Get the last day of the last month and the first day of next month in PHP

Parsing Midnight as Midnight without Losing a Day in C#

Postgres - get midnight from a given timestamp

How to check if timestamp is after today midnight?

SQL - Query Timestamp into Buckets Crossing Midnight

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive