I am trying to convert date time into UTC, on my previous project it was working and giving the correct DateTime as per requirement but when I run the same code on PHP 8.1 I got a different time!
Previous working code (PHP 5.6)-
function convertDateWithTimeZone($src_tz, $dest_tz, $src_date, $date_format = "Y-m-d H:i:s")
{
$dest_date = "";
$curr_tz = date_default_timezone_get();
date_default_timezone_set($src_tz) or die("Can not change the default time zone");
$strtotime = strtotime($src_date);
date_default_timezone_set($dest_tz) or die("Can not change the default time zone");
$dest_date = date($date_format, $strtotime);
date_default_timezone_set($curr_tz) or die("Can not change the default time zone");
return $dest_date;
}
$startdate = "2023-02-11 00:00:00";
$enddate = "2023-02-14 23:59:59";
$fromdate = convertDateWithTimeZone("Asia/Kolkata", "UTC", $startdate, $date_format = "Y-m-d H:i:s");
$todate = convertDateWithTimeZone("Asia/Kolkata", "UTC", $enddate, $date_format = "Y-m-d H:i:s");
I got the result for strtotime($fromdate) is 1676034000.
Now, when I run the same code in PHP 8 I got a different result -
I got the result for strtotime($fromdate) is 1676053800.
Related
I have this function in PHP:
function dt_utc_to_local($dt_utc, $timezone)
{
$dt_utc2 = new DateTimeImmutable($dt_utc, new DateTimeZone('UTC'));
$dt_local_time = $dt_utc2->setTimezone(new DateTimeZone($timezone));
$dt_local_time_formated = $dt_local_time->format('Y-m-d H:i:s');
$dt_now_utc = new DateTimeImmutable(gmdate("Y-m-d H:i:s"), new DateTimeZone('UTC'));
$dt_now_local_time = $dt_now_utc->setTimezone(new DateTimeZone($timezone));
$now_dt_local_time_formated = $dt_now_local_time->format('Y-m-d H:i:s');
return array($now_dt_local_time_formated, $dt_local_time_formated );
}
That I call using:
$msg_date = '2022-09-14 12:00:00';
$timezone = 'America/New_York';
list($dt_now_local_time, $dt_local_time ) = dt_utc_to_local($msg_date, $timezone);
This is to convert UTC datetime that I have in a MySql database to local datetime, but for some reason $dt_now_local_time keeps giving me the date of September 13 and today is 14.
If I use the same code without a function, it works well.
Any idea?
how can I format timestamp from MariaDB to universal time with timezone time from SQL
"2018-08-25 00:00:00"
wanted format
"2018-07-24T08:43:32+02:00"
I already tried this but without any result
$value['date'] = "2018-08-25 00:00:00"
gmdate("Y-m-d\Th:i:s\Z", strtotime($value['date']) + date("Z"))
Use date_format function on DateTime object.
$value['date'] = "2018-08-25 00:00:00";
$datetime = new DateTime($value['date']);
$required_format = date_format($datetime, DATE_ATOM);
echo $required_format;
Rextester Demo
Details:
DATE_ATOM - Atom (example: 2013-04-12T15:52:01+00:00)
I have edited Madhur Bhaiya's code :
<?php
$value['date'] = "2018-08-25 00:00:00";
$timestamped = strtotime($value['date']);
$datetime = new DateTime(strtotime($timestamped));
$required_format = date_format($datetime, DATE_ISO8601);
var_dump($required_format); // returns '2018-09-25T13:37:52+0200'
First off - I am using the MySQLi Procedural method of using MySQL via PHP
I am trying to create a way click a button to set a "timer". From what I understand, the best way to do this (on a dynamic timer - IE: 5 minutes from click) is to calculate what the time() would be 5 minutes from "now".
I want to save this to a DB in case the user disconnects from the page and reconnects (username/password login). This way, when they log back in, it would keep their remaining time.
Now for the code:
I am using this to pull my DT variable from SQL (YYYY-MM-DD HH-MM-SS):
$expirationTime = new DateTime();
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$expirationTime = $row['workTimer'];
}
}
$time = strtotime($expirationTime);
$expirationTime = date("Y-m-d H:i:s", $time);
Then I pull the current time:
$currentTime = date('Y-m-d H:i:s');
I have done echos on both of these and they are displaying correctly. What I want to do now is figure out the difference. I have tried multiple ways of doing this, but nothing seems to work. Most recently I did:
$countdown = $currentTime->diff($expirationTime);
$countdown = $countdown->format("Y-m-d H:i:s", $countdown);
with an error of Fatal error: Call to a member function diff() on a non-object in...
What might be the problem?
Thanks!
I have updated some of the codes per the comment's suggestions to:
$currentTimeT = date('Y-m-d H:i:s');
$currentTime = strtotime($currentTimeT);
$time = date("Y-m-d H:i:s", $expirationTime);
$expirationTime = strtotime($expirationTime);
$countdown = $currentTime->diff($expirationTime);
$countdown = $countdown->format("Y-m-d H:i:s", $countdown);
echo "C ".$currentTime."<br/> E ".$expirationTime."<br/>D ".$countdown;
Same Error.
It is likely the least elegant solution but you could cut your to timestrings both into "d,m,y,h,i,s"-substrings and feed them to http://php.net/manual/de/function.mktime.php. Then you calc the difference of the timestamps.
date_diff — Returns the difference between two DateTime objects
and you are passing non-object to function diff()
See this PHP manual
I solved your issues on my side give it a try:
$time = "2016-09-1 04:45:54";
$time = strtotime($time);
$currentTime = new DateTime(date('Y-m-d H:i:s'));
$expirationTime = new DateTime(date("Y-m-d H:i:s",$time));
$countdown = $currentTime->diff($expirationTime);
$countdown1 = $countdown->format("%H:%I:%S");
print_r($countdown1);die;
Below code is used to get last and next Saturday of current week
// GET Last Satuday of current week
$dt_week_start_time = strtotime("last saturday")+((20*3600)+ 1);
$dt_week_start_date = date('Y-m-d G:i:s', $dt_week_start_time);
// GET Last Satuday of current week
$dt_week_end_time = $dt_week_start_time + (7*3600*24) - 1;
$dt_week_end_date = date('Y-m-d G:i:s', $dt_week_end_time);
Below code is used to convert above both date related variables to EST timezone (default timezone is UTC)
$est_time = new DateTimeZone('EST');
$datetime = new DateTime($dt_week_start_date);
$datetime->setTimezone($est_time);
$dt_week_start_date = $datetime->format('Y-m-d G:i:s');
$datetime = new DateTime($dt_week_end_date);
$datetime->setTimezone($est_time);
$dt_week_end_date = $datetime->format('Y-m-d G:i:s');
Now I want to compare these EST timezone dates with dates in mySQL database table. Dates in mySQL database table are stored in UTC timezone by default. By studying on internet and someone suggested to use CONVERT_TZ function. I tried it but it is giving error like mentioned below
What is wrong in my query? Please advise.
Below 2 queries giving this error: 'Notice: Undefined index: purchasedatetime ...'
$str_query_select = "SELECT CONVERT_TZ(purchasedatetime, 'UTC', 'EST' ) FROM t_product_purchase WHERE sellerpkid=1";
$str_query_select = "SELECT CONVERT_TZ((SELECT purchasedatetime FROM t_product_purchase),'UTC','EST') FROM t_product_purchase WHERE sellerpkid=1";
I want to retrieve date and time from server and according to it do some thing. For this I used following code:
$info = getdate();
$date = $info['mday'];
$month = $info['mon'];
$year = $info['year'];
$hour = $info['hours'];
$min = $info['minutes'];
$sec = $info['seconds'];
$current_date = "$date/$month/$year == $hour:$min:$sec";
This code returns proper date but problem is that what I see in my cpanel(server time) is diff. than what I get from code. In cpanel time is CDT while from code it is showing UTC which I reconfirm using following code
<?php echo date("r == e"); ?>
Why this is happening and what changes I have to do in my code so that I can get proper server time.
You should set the timezone to the one of the timezones you want.
// set default timezone
date_default_timezone_set('America/Chicago'); // CDT
$info = getdate();
$date = $info['mday'];
$month = $info['mon'];
$year = $info['year'];
$hour = $info['hours'];
$min = $info['minutes'];
$sec = $info['seconds'];
$current_date = "$date/$month/$year == $hour:$min:$sec";
Or a much shorter version:
// set default timezone
date_default_timezone_set('America/Chicago'); // CDT
$current_date = date('d/m/Y == H:i:s');
Try this -
<?php
date_default_timezone_set('Asia/Kolkata');
$timestamp = time();
$date_time = date("d-m-Y (D) H:i:s", $timestamp);
echo "Current date and local time on this server is $date_time";
?>
No need to use date_default_timezone_set for the whole script, just specify the timezone you want with a DateTime object:
$now = new DateTime(null, new DateTimeZone('America/New_York'));
$now->setTimezone(new DateTimeZone('Europe/London')); // Another way
echo $now->format("Y-m-d\TH:i:sO"); // something like "2015-02-11T06:16:47+0100" (ISO 8601)
You have to set the timezone, cf http://www.php.net/manual/en/book.datetime.php
For enable PHP Extension intl , follow the Steps..
Open the xampp/php/php.ini file in any editor.
Search ";extension=php_intl.dll"
kindly remove the starting semicolon ( ; ) Like : ;extension=php_intl.dll. to.
extension=php_intl.dll.
Save the xampp/php/php.ini file.
Restart your xampp/wamp.
You should set the timezone to the one of the timezones you want. let set the Indian timezone
// set default timezone
date_default_timezone_set('Asia/Kolkata');
$info = getdate();
$date = $info['mday'];
$month = $info['mon'];
$year = $info['year'];
$hour = $info['hours'];
$min = $info['minutes'];
$sec = $info['seconds'];
$current_date = "$date/$month/$year == $hour:$min:$sec";
You can use the "system( string $command[, int &$return_var] ) : string" function. For Windows system( "time" );, system( "time > output.txt" ); to put the response in the output.txt file. If you plan on deploying your website on someone else's server, then you may not want to hardcode the server time, as the server may move to an unknown timezone. Then it may be best to set the default time zone in the php.ini file. I use Hostinger and they allow you to configure that php.ini value.