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?
Related
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.
I want to add variable of type TimeType to a variable of DateTimeType
I have triying this code :
$newDate = $dateAppointment->add($duration);
but i got this error
Warning: DateTime::add() expects parameter 1 to be DateInterval,
object given
Examples of data:
$dateAppountment = 2019-03-21 10:15:00
$duration : 00:15:00
I suppose there's some kind of transformer already exists in symfony, but I can't find it. So, here's a custom code how to convert time from DateTime to DateInterval and add it to another \DateTime:
$dateAppointment = (new \DateTime());
$dtDuration = (new \DateTime())->setTime(1, 15, 0);
$duration = $duration->format('\P\TH\Hi\Ms\S');
$newDate = $dateAppointment->add(new \DateInterval($duration));
Fiddle: https://3v4l.org/5lFlQ
Here is an example:
$duration = '1970-01-01 01:00:00.000000';
$timeObject = DateTime::createFromFormat('Y-m-d H:i:s.u', $duration, new DateTimeZone('UTC'));
$date = '2019-03-21 10:15:00';
$dateObject = DateTime::createFromFormat('Y-m-d H:i:s', $date);
$modify = '+' . $timeObject->format('U') . ' second';
echo $dateObject->modify($modify)->format('Y-m-d H:i:s');
Just get the seconds from your duration object and add them to your date object. If you used modify, there are a lot of things you can do.
I'm using the PHP DateTime class to generate a date for a custom licensing system. While I'm debugging it, I've noted that the date time are always wrong, it will be 5-dec-2018 but now we are in November, and this date will be the same also for the expiration_date.
How i can fix this issue? I need to add 30 days to the start date of the trial period.
Here is the code.
class Activator {
private $uuid;
private $keygen;
private $licence_code;
public static function generateLicence($uuid) {
if (!file_exists(ABSPATH.'/DataStorage/.licence')) {
$start_date = new DateTime();
$time_zone = $start_date->setTimezone(new DateTimeZone('Europe/Rome'));
$trial_date = $start_date->add(new DateInterval('P30D'));
$end_date = $trial_date->format('d-M-Y');
$machine_uuid = bin2hex($uuid);
$licence_code = base64_encode($machine_uuid);
$licence_file = array(
'uuid' => $machine_uuid,
'activation_date' => $time_zone->format('d-M-Y'),
#'trial_version' => true,
#'expire_date' => $end_date,
#'licence_code' => $licence_code
);
$w = file_put_contents(ABSPATH.'/DataStorage/.licence', json_encode($licence_file));
echo $w;
}
}
This is the expected behavior, as you add() to the date (by doing $start_date->add(...) - this modifies the original object $start_date.
You can solve this a few different ways, although the easiest way is just to create a new instance entirely with the 30 days added directly in the construct. You can also set the timezone as the second parameter of new DateTime().
$timezone = new DateTimeZone('Europe/Rome');
$start_date = new DateTime("now", $timezone);
$trial_date = new DateTime("+30 days", $timezone);
PHP.net on new DateTime()
PHP.net on DateTime::add()
See this live demo.
I am currently working on displaying a moment. I want the view to use DateTime->format(..). The value I am getting from the API is 1502462223168. However, this is displayed as 1945-5-26 16:36 instead of 2017-8-11 16:37, since the original value exceeds the PHP_MAX_INT value on the system.
Is there a way I can use the original value, maybe as a String, to create the DateTime object?
since your timestamp value is in milliseconds, divide it by 1000 and then use DateTime, like:
$date = new DateTime();
$stamp = intval(1502462223168/1000);
$date = DateTime::createFromFormat("U", $stamp)->format("Y-m-d H:i:s");
echo $date;
Use this it will be working fine for your question
$timestamp = 1465298940;
$datetimeFormat = 'Y-m-d H:i:s';
$date = new DateTime();
// If you must have use time zones
// $date = new \DateTime('now', new \DateTimeZone('Europe/Helsinki'));
$date->setTimestamp($timestamp);
echo $date->format($datetimeFormat);
My code is:
$etime = $arr['od'];
$ds = date("Y-m-d H:i:s", strtotime($etime));
$timezone=new DateTimeZone("UTC"); // declare whatever your timezone is
$etimed=new DateTime($ds,$timezone); // resultset datetime
$arr['od'] is 2017-08-20 19:05:59, trying to echo $etimed it breaks my code but I don't know why?
Datetime is an object, it cannot be echoed like that, therefore you need to format the datetime to echo it like the code below:
$etime = $arr['od'];
$ds = date("Y-m-d H:i:s", strtotime($etime));
$timezone=new DateTimeZone("UTC"); // declare whatever your timezone is
$etimed=new DateTime($ds,$timezone); // resultset datetime
echo $etimed->format('Y-m-d H:i:s');