PHP Timestamp Changes before midnight to next day
![Multi tool use Multi tool use](http://sgv.ssvwv.com/sg/ssvwvcomimagb.png)
Multi tool use
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.
php wordpress timestamp
add a comment |
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.
php wordpress timestamp
What do you mean by early. 10 minutes or 6 hours?
– Andreas
Jan 2 at 16:05
first of all get your current default timezone with this function date_default_timezone_get() , I think your current timezone is America/Denver
– ttrasn
Jan 2 at 16:11
What do you mean by "get my timestamp to change at midnight to the next day" ? When it's 12 PM the time will start to be in the next day normally. Can you explain more what are you trying to achieve ?
– Accountant م
Jan 2 at 16:17
As ttrasn suggested, first get your current time zone, then make sure that is not overwritten by other configuration files. This set timezone for the current script: date_default_timezone_set('America/Denver'); NOTE: Mountain .......... America/Denver; Mountain no DST ... America/Phoenix
– TDK
Jan 2 at 16:22
I meant that the date changes about 6 hours prior to midnight....so Im trying to get it accurately to change at midnight for the Mountain Time Zone.
– TractionW
Jan 2 at 17:25
add a comment |
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.
php wordpress timestamp
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.
php wordpress timestamp
php wordpress timestamp
asked Jan 2 at 15:57
![](https://lh4.googleusercontent.com/-CALcQO12LbY/AAAAAAAAAAI/AAAAAAAAAAc/IKZJ14jdbTM/photo.jpg?sz=32)
![](https://lh4.googleusercontent.com/-CALcQO12LbY/AAAAAAAAAAI/AAAAAAAAAAc/IKZJ14jdbTM/photo.jpg?sz=32)
TractionWTractionW
31
31
What do you mean by early. 10 minutes or 6 hours?
– Andreas
Jan 2 at 16:05
first of all get your current default timezone with this function date_default_timezone_get() , I think your current timezone is America/Denver
– ttrasn
Jan 2 at 16:11
What do you mean by "get my timestamp to change at midnight to the next day" ? When it's 12 PM the time will start to be in the next day normally. Can you explain more what are you trying to achieve ?
– Accountant م
Jan 2 at 16:17
As ttrasn suggested, first get your current time zone, then make sure that is not overwritten by other configuration files. This set timezone for the current script: date_default_timezone_set('America/Denver'); NOTE: Mountain .......... America/Denver; Mountain no DST ... America/Phoenix
– TDK
Jan 2 at 16:22
I meant that the date changes about 6 hours prior to midnight....so Im trying to get it accurately to change at midnight for the Mountain Time Zone.
– TractionW
Jan 2 at 17:25
add a comment |
What do you mean by early. 10 minutes or 6 hours?
– Andreas
Jan 2 at 16:05
first of all get your current default timezone with this function date_default_timezone_get() , I think your current timezone is America/Denver
– ttrasn
Jan 2 at 16:11
What do you mean by "get my timestamp to change at midnight to the next day" ? When it's 12 PM the time will start to be in the next day normally. Can you explain more what are you trying to achieve ?
– Accountant م
Jan 2 at 16:17
As ttrasn suggested, first get your current time zone, then make sure that is not overwritten by other configuration files. This set timezone for the current script: date_default_timezone_set('America/Denver'); NOTE: Mountain .......... America/Denver; Mountain no DST ... America/Phoenix
– TDK
Jan 2 at 16:22
I meant that the date changes about 6 hours prior to midnight....so Im trying to get it accurately to change at midnight for the Mountain Time Zone.
– TractionW
Jan 2 at 17:25
What do you mean by early. 10 minutes or 6 hours?
– Andreas
Jan 2 at 16:05
What do you mean by early. 10 minutes or 6 hours?
– Andreas
Jan 2 at 16:05
first of all get your current default timezone with this function date_default_timezone_get() , I think your current timezone is America/Denver
– ttrasn
Jan 2 at 16:11
first of all get your current default timezone with this function date_default_timezone_get() , I think your current timezone is America/Denver
– ttrasn
Jan 2 at 16:11
What do you mean by "get my timestamp to change at midnight to the next day" ? When it's 12 PM the time will start to be in the next day normally. Can you explain more what are you trying to achieve ?
– Accountant م
Jan 2 at 16:17
What do you mean by "get my timestamp to change at midnight to the next day" ? When it's 12 PM the time will start to be in the next day normally. Can you explain more what are you trying to achieve ?
– Accountant م
Jan 2 at 16:17
As ttrasn suggested, first get your current time zone, then make sure that is not overwritten by other configuration files. This set timezone for the current script: date_default_timezone_set('America/Denver'); NOTE: Mountain .......... America/Denver; Mountain no DST ... America/Phoenix
– TDK
Jan 2 at 16:22
As ttrasn suggested, first get your current time zone, then make sure that is not overwritten by other configuration files. This set timezone for the current script: date_default_timezone_set('America/Denver'); NOTE: Mountain .......... America/Denver; Mountain no DST ... America/Phoenix
– TDK
Jan 2 at 16:22
I meant that the date changes about 6 hours prior to midnight....so Im trying to get it accurately to change at midnight for the Mountain Time Zone.
– TractionW
Jan 2 at 17:25
I meant that the date changes about 6 hours prior to midnight....so Im trying to get it accurately to change at midnight for the Mountain Time Zone.
– TractionW
Jan 2 at 17:25
add a comment |
2 Answers
2
active
oldest
votes
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
Thank you for this! One question...I added the DateTime object and the time is about 6 hours ahead. It looks like its rendering Universal time and not local time. Am I missing a step?
– TractionW
Jan 2 at 17:23
Got it to work with the second snippet you provided. Thank you so much!
– TractionW
Jan 2 at 18:00
@TractionW check also that the server is providing a valid Universal time value, the last value of the time formatted is the time offset that depends on the local time (09:11:36 -0700)
– David Lemon
Jan 3 at 8:41
add a comment |
Function that return exact timezone difference along with update time
including DayLight Saving. Feel free to contact for further
assistance.
/*
* @function minusonesecond return timezone
* @param integer $timezonedifference optional
* @return string time
* @Comment Get User TimeZone Difference
*/
public function minusonesecond($timezonedifference=""){
$i=0;
$time = explode(":", $timezonedifference);
if($time[0]<0){$sign=-1; }else {$sign =1;}
foreach ($time as $endtmp){
$endtmp = $sign*$endtmp;
if($i>0){
$tmp[$i] = (($endtmp*1==0) ? 59: ($endtmp-1));
} else $tmp[$i] = ($endtmp-1);
$i++;
}
$ret = ($sign)* $tmp[0].":".(($tmp[1]>9)?$tmp[1]:"0$tmp[1]").":".(($tmp[2]>9)?$tmp[2]:"0$tmp[2]");
return $ret ;
}
/**
* @function getusertimezonedifference return timezone difference with UTC
* @param integer $timezonedifference optional
* @return string time
* @Comment Get User TimeZone Difference
*/
public function getusertimezonedifference($user_id = "", $changetoUTC=false, $flg_array=false)
{
$user = $this->User->find("first", array("conditions" => array("User.user_id" => $user_id)));
$timezone = $user["Timezone"]["timezone_name"];
$dayLightFlag = false;
$system_timezone = date_default_timezone_get();
$local_timezone = "UTC";
date_default_timezone_set($local_timezone);
$local = date("Y-m-d H:i:s");
date_default_timezone_set("GMT");
$gmt = date("Y-m-d H:i:s");
date_default_timezone_set($timezone);
$required = date("Y-m-d H:i:s");
date_default_timezone_set($system_timezone);
$diff1 = (strtotime($gmt) - strtotime($local));
$diff2 = (strtotime($required) - strtotime($gmt));
if($changetoUTC) {$diff2 = (-1*$diff2);}
// extract hours
$hours = floor($diff2 / (60 * 60));
// extract minutes
$divisor_for_minutes = $diff2 % (60 * 60);
$minutes = floor($divisor_for_minutes / 60);
// extract the remaining seconds
$divisor_for_seconds = $divisor_for_minutes % 60;
$seconds = ceil($divisor_for_seconds);
//create string HH:MM:SS
$ret = $hours.":".(($minutes>9)?$minutes:"0$minutes").":".(($seconds>9)?$seconds:"0$seconds");
if($flg_array){$ret = array($hours, (($minutes>9)?$minutes:"0$minutes"), ($seconds>9)?$seconds:"0$seconds");}
return ($ret);
}
/**
* @function getPSTtimezonedifference return timezone difference with UTC AND PST
* @param $changetoUTC optional
* @param $flg_array optional
* @return string time
* @Comment Get User TimeZone Difference
*/
public function getPSTtimezonedifference($changetoUTC=false, $flg_array=false)
{
$timezone = "America/Los_Angeles";
$dayLightFlag = false;
$system_timezone = date_default_timezone_get();
$local_timezone = "UTC";
date_default_timezone_set($local_timezone);
$local = date("Y-m-d H:i:s");
date_default_timezone_set("GMT");
$gmt = date("Y-m-d H:i:s");
date_default_timezone_set($timezone);
$required = date("Y-m-d H:i:s");
date_default_timezone_set($system_timezone);
$diff1 = (strtotime($gmt) - strtotime($local));
$diff2 = (strtotime($required) - strtotime($gmt));
if($changetoUTC) {$diff2 = (-1*$diff2);}
// extract hours
$hours = floor($diff2 / (60 * 60));
// extract minutes
$divisor_for_minutes = $diff2 % (60 * 60);
$minutes = floor($divisor_for_minutes / 60);
// extract the remaining seconds
$divisor_for_seconds = $divisor_for_minutes % 60;
$seconds = ceil($divisor_for_seconds);
//create string HH:MM:SS
$ret = $hours.":".(($minutes>9)?$minutes:"0$minutes").":".(($seconds>9)?$seconds:"0$seconds");
if($flg_array){$ret = array($hours, (($minutes>9)?$minutes:"0$minutes"), ($seconds>9)?$seconds:"0$seconds");}
return ($ret);
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54009359%2fphp-timestamp-changes-before-midnight-to-next-day%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
Thank you for this! One question...I added the DateTime object and the time is about 6 hours ahead. It looks like its rendering Universal time and not local time. Am I missing a step?
– TractionW
Jan 2 at 17:23
Got it to work with the second snippet you provided. Thank you so much!
– TractionW
Jan 2 at 18:00
@TractionW check also that the server is providing a valid Universal time value, the last value of the time formatted is the time offset that depends on the local time (09:11:36 -0700)
– David Lemon
Jan 3 at 8:41
add a comment |
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
Thank you for this! One question...I added the DateTime object and the time is about 6 hours ahead. It looks like its rendering Universal time and not local time. Am I missing a step?
– TractionW
Jan 2 at 17:23
Got it to work with the second snippet you provided. Thank you so much!
– TractionW
Jan 2 at 18:00
@TractionW check also that the server is providing a valid Universal time value, the last value of the time formatted is the time offset that depends on the local time (09:11:36 -0700)
– David Lemon
Jan 3 at 8:41
add a comment |
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
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
answered Jan 2 at 16:36
David LemonDavid Lemon
1,061718
1,061718
Thank you for this! One question...I added the DateTime object and the time is about 6 hours ahead. It looks like its rendering Universal time and not local time. Am I missing a step?
– TractionW
Jan 2 at 17:23
Got it to work with the second snippet you provided. Thank you so much!
– TractionW
Jan 2 at 18:00
@TractionW check also that the server is providing a valid Universal time value, the last value of the time formatted is the time offset that depends on the local time (09:11:36 -0700)
– David Lemon
Jan 3 at 8:41
add a comment |
Thank you for this! One question...I added the DateTime object and the time is about 6 hours ahead. It looks like its rendering Universal time and not local time. Am I missing a step?
– TractionW
Jan 2 at 17:23
Got it to work with the second snippet you provided. Thank you so much!
– TractionW
Jan 2 at 18:00
@TractionW check also that the server is providing a valid Universal time value, the last value of the time formatted is the time offset that depends on the local time (09:11:36 -0700)
– David Lemon
Jan 3 at 8:41
Thank you for this! One question...I added the DateTime object and the time is about 6 hours ahead. It looks like its rendering Universal time and not local time. Am I missing a step?
– TractionW
Jan 2 at 17:23
Thank you for this! One question...I added the DateTime object and the time is about 6 hours ahead. It looks like its rendering Universal time and not local time. Am I missing a step?
– TractionW
Jan 2 at 17:23
Got it to work with the second snippet you provided. Thank you so much!
– TractionW
Jan 2 at 18:00
Got it to work with the second snippet you provided. Thank you so much!
– TractionW
Jan 2 at 18:00
@TractionW check also that the server is providing a valid Universal time value, the last value of the time formatted is the time offset that depends on the local time (09:11:36 -0700)
– David Lemon
Jan 3 at 8:41
@TractionW check also that the server is providing a valid Universal time value, the last value of the time formatted is the time offset that depends on the local time (09:11:36 -0700)
– David Lemon
Jan 3 at 8:41
add a comment |
Function that return exact timezone difference along with update time
including DayLight Saving. Feel free to contact for further
assistance.
/*
* @function minusonesecond return timezone
* @param integer $timezonedifference optional
* @return string time
* @Comment Get User TimeZone Difference
*/
public function minusonesecond($timezonedifference=""){
$i=0;
$time = explode(":", $timezonedifference);
if($time[0]<0){$sign=-1; }else {$sign =1;}
foreach ($time as $endtmp){
$endtmp = $sign*$endtmp;
if($i>0){
$tmp[$i] = (($endtmp*1==0) ? 59: ($endtmp-1));
} else $tmp[$i] = ($endtmp-1);
$i++;
}
$ret = ($sign)* $tmp[0].":".(($tmp[1]>9)?$tmp[1]:"0$tmp[1]").":".(($tmp[2]>9)?$tmp[2]:"0$tmp[2]");
return $ret ;
}
/**
* @function getusertimezonedifference return timezone difference with UTC
* @param integer $timezonedifference optional
* @return string time
* @Comment Get User TimeZone Difference
*/
public function getusertimezonedifference($user_id = "", $changetoUTC=false, $flg_array=false)
{
$user = $this->User->find("first", array("conditions" => array("User.user_id" => $user_id)));
$timezone = $user["Timezone"]["timezone_name"];
$dayLightFlag = false;
$system_timezone = date_default_timezone_get();
$local_timezone = "UTC";
date_default_timezone_set($local_timezone);
$local = date("Y-m-d H:i:s");
date_default_timezone_set("GMT");
$gmt = date("Y-m-d H:i:s");
date_default_timezone_set($timezone);
$required = date("Y-m-d H:i:s");
date_default_timezone_set($system_timezone);
$diff1 = (strtotime($gmt) - strtotime($local));
$diff2 = (strtotime($required) - strtotime($gmt));
if($changetoUTC) {$diff2 = (-1*$diff2);}
// extract hours
$hours = floor($diff2 / (60 * 60));
// extract minutes
$divisor_for_minutes = $diff2 % (60 * 60);
$minutes = floor($divisor_for_minutes / 60);
// extract the remaining seconds
$divisor_for_seconds = $divisor_for_minutes % 60;
$seconds = ceil($divisor_for_seconds);
//create string HH:MM:SS
$ret = $hours.":".(($minutes>9)?$minutes:"0$minutes").":".(($seconds>9)?$seconds:"0$seconds");
if($flg_array){$ret = array($hours, (($minutes>9)?$minutes:"0$minutes"), ($seconds>9)?$seconds:"0$seconds");}
return ($ret);
}
/**
* @function getPSTtimezonedifference return timezone difference with UTC AND PST
* @param $changetoUTC optional
* @param $flg_array optional
* @return string time
* @Comment Get User TimeZone Difference
*/
public function getPSTtimezonedifference($changetoUTC=false, $flg_array=false)
{
$timezone = "America/Los_Angeles";
$dayLightFlag = false;
$system_timezone = date_default_timezone_get();
$local_timezone = "UTC";
date_default_timezone_set($local_timezone);
$local = date("Y-m-d H:i:s");
date_default_timezone_set("GMT");
$gmt = date("Y-m-d H:i:s");
date_default_timezone_set($timezone);
$required = date("Y-m-d H:i:s");
date_default_timezone_set($system_timezone);
$diff1 = (strtotime($gmt) - strtotime($local));
$diff2 = (strtotime($required) - strtotime($gmt));
if($changetoUTC) {$diff2 = (-1*$diff2);}
// extract hours
$hours = floor($diff2 / (60 * 60));
// extract minutes
$divisor_for_minutes = $diff2 % (60 * 60);
$minutes = floor($divisor_for_minutes / 60);
// extract the remaining seconds
$divisor_for_seconds = $divisor_for_minutes % 60;
$seconds = ceil($divisor_for_seconds);
//create string HH:MM:SS
$ret = $hours.":".(($minutes>9)?$minutes:"0$minutes").":".(($seconds>9)?$seconds:"0$seconds");
if($flg_array){$ret = array($hours, (($minutes>9)?$minutes:"0$minutes"), ($seconds>9)?$seconds:"0$seconds");}
return ($ret);
}
add a comment |
Function that return exact timezone difference along with update time
including DayLight Saving. Feel free to contact for further
assistance.
/*
* @function minusonesecond return timezone
* @param integer $timezonedifference optional
* @return string time
* @Comment Get User TimeZone Difference
*/
public function minusonesecond($timezonedifference=""){
$i=0;
$time = explode(":", $timezonedifference);
if($time[0]<0){$sign=-1; }else {$sign =1;}
foreach ($time as $endtmp){
$endtmp = $sign*$endtmp;
if($i>0){
$tmp[$i] = (($endtmp*1==0) ? 59: ($endtmp-1));
} else $tmp[$i] = ($endtmp-1);
$i++;
}
$ret = ($sign)* $tmp[0].":".(($tmp[1]>9)?$tmp[1]:"0$tmp[1]").":".(($tmp[2]>9)?$tmp[2]:"0$tmp[2]");
return $ret ;
}
/**
* @function getusertimezonedifference return timezone difference with UTC
* @param integer $timezonedifference optional
* @return string time
* @Comment Get User TimeZone Difference
*/
public function getusertimezonedifference($user_id = "", $changetoUTC=false, $flg_array=false)
{
$user = $this->User->find("first", array("conditions" => array("User.user_id" => $user_id)));
$timezone = $user["Timezone"]["timezone_name"];
$dayLightFlag = false;
$system_timezone = date_default_timezone_get();
$local_timezone = "UTC";
date_default_timezone_set($local_timezone);
$local = date("Y-m-d H:i:s");
date_default_timezone_set("GMT");
$gmt = date("Y-m-d H:i:s");
date_default_timezone_set($timezone);
$required = date("Y-m-d H:i:s");
date_default_timezone_set($system_timezone);
$diff1 = (strtotime($gmt) - strtotime($local));
$diff2 = (strtotime($required) - strtotime($gmt));
if($changetoUTC) {$diff2 = (-1*$diff2);}
// extract hours
$hours = floor($diff2 / (60 * 60));
// extract minutes
$divisor_for_minutes = $diff2 % (60 * 60);
$minutes = floor($divisor_for_minutes / 60);
// extract the remaining seconds
$divisor_for_seconds = $divisor_for_minutes % 60;
$seconds = ceil($divisor_for_seconds);
//create string HH:MM:SS
$ret = $hours.":".(($minutes>9)?$minutes:"0$minutes").":".(($seconds>9)?$seconds:"0$seconds");
if($flg_array){$ret = array($hours, (($minutes>9)?$minutes:"0$minutes"), ($seconds>9)?$seconds:"0$seconds");}
return ($ret);
}
/**
* @function getPSTtimezonedifference return timezone difference with UTC AND PST
* @param $changetoUTC optional
* @param $flg_array optional
* @return string time
* @Comment Get User TimeZone Difference
*/
public function getPSTtimezonedifference($changetoUTC=false, $flg_array=false)
{
$timezone = "America/Los_Angeles";
$dayLightFlag = false;
$system_timezone = date_default_timezone_get();
$local_timezone = "UTC";
date_default_timezone_set($local_timezone);
$local = date("Y-m-d H:i:s");
date_default_timezone_set("GMT");
$gmt = date("Y-m-d H:i:s");
date_default_timezone_set($timezone);
$required = date("Y-m-d H:i:s");
date_default_timezone_set($system_timezone);
$diff1 = (strtotime($gmt) - strtotime($local));
$diff2 = (strtotime($required) - strtotime($gmt));
if($changetoUTC) {$diff2 = (-1*$diff2);}
// extract hours
$hours = floor($diff2 / (60 * 60));
// extract minutes
$divisor_for_minutes = $diff2 % (60 * 60);
$minutes = floor($divisor_for_minutes / 60);
// extract the remaining seconds
$divisor_for_seconds = $divisor_for_minutes % 60;
$seconds = ceil($divisor_for_seconds);
//create string HH:MM:SS
$ret = $hours.":".(($minutes>9)?$minutes:"0$minutes").":".(($seconds>9)?$seconds:"0$seconds");
if($flg_array){$ret = array($hours, (($minutes>9)?$minutes:"0$minutes"), ($seconds>9)?$seconds:"0$seconds");}
return ($ret);
}
add a comment |
Function that return exact timezone difference along with update time
including DayLight Saving. Feel free to contact for further
assistance.
/*
* @function minusonesecond return timezone
* @param integer $timezonedifference optional
* @return string time
* @Comment Get User TimeZone Difference
*/
public function minusonesecond($timezonedifference=""){
$i=0;
$time = explode(":", $timezonedifference);
if($time[0]<0){$sign=-1; }else {$sign =1;}
foreach ($time as $endtmp){
$endtmp = $sign*$endtmp;
if($i>0){
$tmp[$i] = (($endtmp*1==0) ? 59: ($endtmp-1));
} else $tmp[$i] = ($endtmp-1);
$i++;
}
$ret = ($sign)* $tmp[0].":".(($tmp[1]>9)?$tmp[1]:"0$tmp[1]").":".(($tmp[2]>9)?$tmp[2]:"0$tmp[2]");
return $ret ;
}
/**
* @function getusertimezonedifference return timezone difference with UTC
* @param integer $timezonedifference optional
* @return string time
* @Comment Get User TimeZone Difference
*/
public function getusertimezonedifference($user_id = "", $changetoUTC=false, $flg_array=false)
{
$user = $this->User->find("first", array("conditions" => array("User.user_id" => $user_id)));
$timezone = $user["Timezone"]["timezone_name"];
$dayLightFlag = false;
$system_timezone = date_default_timezone_get();
$local_timezone = "UTC";
date_default_timezone_set($local_timezone);
$local = date("Y-m-d H:i:s");
date_default_timezone_set("GMT");
$gmt = date("Y-m-d H:i:s");
date_default_timezone_set($timezone);
$required = date("Y-m-d H:i:s");
date_default_timezone_set($system_timezone);
$diff1 = (strtotime($gmt) - strtotime($local));
$diff2 = (strtotime($required) - strtotime($gmt));
if($changetoUTC) {$diff2 = (-1*$diff2);}
// extract hours
$hours = floor($diff2 / (60 * 60));
// extract minutes
$divisor_for_minutes = $diff2 % (60 * 60);
$minutes = floor($divisor_for_minutes / 60);
// extract the remaining seconds
$divisor_for_seconds = $divisor_for_minutes % 60;
$seconds = ceil($divisor_for_seconds);
//create string HH:MM:SS
$ret = $hours.":".(($minutes>9)?$minutes:"0$minutes").":".(($seconds>9)?$seconds:"0$seconds");
if($flg_array){$ret = array($hours, (($minutes>9)?$minutes:"0$minutes"), ($seconds>9)?$seconds:"0$seconds");}
return ($ret);
}
/**
* @function getPSTtimezonedifference return timezone difference with UTC AND PST
* @param $changetoUTC optional
* @param $flg_array optional
* @return string time
* @Comment Get User TimeZone Difference
*/
public function getPSTtimezonedifference($changetoUTC=false, $flg_array=false)
{
$timezone = "America/Los_Angeles";
$dayLightFlag = false;
$system_timezone = date_default_timezone_get();
$local_timezone = "UTC";
date_default_timezone_set($local_timezone);
$local = date("Y-m-d H:i:s");
date_default_timezone_set("GMT");
$gmt = date("Y-m-d H:i:s");
date_default_timezone_set($timezone);
$required = date("Y-m-d H:i:s");
date_default_timezone_set($system_timezone);
$diff1 = (strtotime($gmt) - strtotime($local));
$diff2 = (strtotime($required) - strtotime($gmt));
if($changetoUTC) {$diff2 = (-1*$diff2);}
// extract hours
$hours = floor($diff2 / (60 * 60));
// extract minutes
$divisor_for_minutes = $diff2 % (60 * 60);
$minutes = floor($divisor_for_minutes / 60);
// extract the remaining seconds
$divisor_for_seconds = $divisor_for_minutes % 60;
$seconds = ceil($divisor_for_seconds);
//create string HH:MM:SS
$ret = $hours.":".(($minutes>9)?$minutes:"0$minutes").":".(($seconds>9)?$seconds:"0$seconds");
if($flg_array){$ret = array($hours, (($minutes>9)?$minutes:"0$minutes"), ($seconds>9)?$seconds:"0$seconds");}
return ($ret);
}
Function that return exact timezone difference along with update time
including DayLight Saving. Feel free to contact for further
assistance.
/*
* @function minusonesecond return timezone
* @param integer $timezonedifference optional
* @return string time
* @Comment Get User TimeZone Difference
*/
public function minusonesecond($timezonedifference=""){
$i=0;
$time = explode(":", $timezonedifference);
if($time[0]<0){$sign=-1; }else {$sign =1;}
foreach ($time as $endtmp){
$endtmp = $sign*$endtmp;
if($i>0){
$tmp[$i] = (($endtmp*1==0) ? 59: ($endtmp-1));
} else $tmp[$i] = ($endtmp-1);
$i++;
}
$ret = ($sign)* $tmp[0].":".(($tmp[1]>9)?$tmp[1]:"0$tmp[1]").":".(($tmp[2]>9)?$tmp[2]:"0$tmp[2]");
return $ret ;
}
/**
* @function getusertimezonedifference return timezone difference with UTC
* @param integer $timezonedifference optional
* @return string time
* @Comment Get User TimeZone Difference
*/
public function getusertimezonedifference($user_id = "", $changetoUTC=false, $flg_array=false)
{
$user = $this->User->find("first", array("conditions" => array("User.user_id" => $user_id)));
$timezone = $user["Timezone"]["timezone_name"];
$dayLightFlag = false;
$system_timezone = date_default_timezone_get();
$local_timezone = "UTC";
date_default_timezone_set($local_timezone);
$local = date("Y-m-d H:i:s");
date_default_timezone_set("GMT");
$gmt = date("Y-m-d H:i:s");
date_default_timezone_set($timezone);
$required = date("Y-m-d H:i:s");
date_default_timezone_set($system_timezone);
$diff1 = (strtotime($gmt) - strtotime($local));
$diff2 = (strtotime($required) - strtotime($gmt));
if($changetoUTC) {$diff2 = (-1*$diff2);}
// extract hours
$hours = floor($diff2 / (60 * 60));
// extract minutes
$divisor_for_minutes = $diff2 % (60 * 60);
$minutes = floor($divisor_for_minutes / 60);
// extract the remaining seconds
$divisor_for_seconds = $divisor_for_minutes % 60;
$seconds = ceil($divisor_for_seconds);
//create string HH:MM:SS
$ret = $hours.":".(($minutes>9)?$minutes:"0$minutes").":".(($seconds>9)?$seconds:"0$seconds");
if($flg_array){$ret = array($hours, (($minutes>9)?$minutes:"0$minutes"), ($seconds>9)?$seconds:"0$seconds");}
return ($ret);
}
/**
* @function getPSTtimezonedifference return timezone difference with UTC AND PST
* @param $changetoUTC optional
* @param $flg_array optional
* @return string time
* @Comment Get User TimeZone Difference
*/
public function getPSTtimezonedifference($changetoUTC=false, $flg_array=false)
{
$timezone = "America/Los_Angeles";
$dayLightFlag = false;
$system_timezone = date_default_timezone_get();
$local_timezone = "UTC";
date_default_timezone_set($local_timezone);
$local = date("Y-m-d H:i:s");
date_default_timezone_set("GMT");
$gmt = date("Y-m-d H:i:s");
date_default_timezone_set($timezone);
$required = date("Y-m-d H:i:s");
date_default_timezone_set($system_timezone);
$diff1 = (strtotime($gmt) - strtotime($local));
$diff2 = (strtotime($required) - strtotime($gmt));
if($changetoUTC) {$diff2 = (-1*$diff2);}
// extract hours
$hours = floor($diff2 / (60 * 60));
// extract minutes
$divisor_for_minutes = $diff2 % (60 * 60);
$minutes = floor($divisor_for_minutes / 60);
// extract the remaining seconds
$divisor_for_seconds = $divisor_for_minutes % 60;
$seconds = ceil($divisor_for_seconds);
//create string HH:MM:SS
$ret = $hours.":".(($minutes>9)?$minutes:"0$minutes").":".(($seconds>9)?$seconds:"0$seconds");
if($flg_array){$ret = array($hours, (($minutes>9)?$minutes:"0$minutes"), ($seconds>9)?$seconds:"0$seconds");}
return ($ret);
}
answered Feb 12 at 6:43
![](https://i.stack.imgur.com/QM99U.jpg?s=32&g=1)
![](https://i.stack.imgur.com/QM99U.jpg?s=32&g=1)
Abdul RehmanAbdul Rehman
581511
581511
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54009359%2fphp-timestamp-changes-before-midnight-to-next-day%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3,QrmkiJo,UfhTx5YXEYGuzWFDYmA48 ANKiEGe0Gj8N RT6cEitb0G 8yY4 JHCCEGBQSABmE,1FiEWpdyS
What do you mean by early. 10 minutes or 6 hours?
– Andreas
Jan 2 at 16:05
first of all get your current default timezone with this function date_default_timezone_get() , I think your current timezone is America/Denver
– ttrasn
Jan 2 at 16:11
What do you mean by "get my timestamp to change at midnight to the next day" ? When it's 12 PM the time will start to be in the next day normally. Can you explain more what are you trying to achieve ?
– Accountant م
Jan 2 at 16:17
As ttrasn suggested, first get your current time zone, then make sure that is not overwritten by other configuration files. This set timezone for the current script: date_default_timezone_set('America/Denver'); NOTE: Mountain .......... America/Denver; Mountain no DST ... America/Phoenix
– TDK
Jan 2 at 16:22
I meant that the date changes about 6 hours prior to midnight....so Im trying to get it accurately to change at midnight for the Mountain Time Zone.
– TractionW
Jan 2 at 17:25