How to return Date value - php

Consider this simple code:
$datetime = new DateTime('2015-12-12 23-23', new DateTimeZone('UTC'));
$timezone = new DateTimeZone('Europe/London');
$datetime->setTimezone($timezone);
Now I want to display the date in Europe/London time zone I've just applied but without giving any date format. I want the date format to be the same as input date.
This: $datetime->format() doesn't work. Cannot find other method than format() to get this date :/

you can simply use date not DateTime
<?php
date_default_timezone_set('Europe/London');
echo date('Y-m-d H-i');

Related

Convert PHP Mongo UTC Date From Milliseconds To Date for TimeZone

I have a date in UTC / Epoch format from a mongo database (1659052800000) and I'm trying to convert it to a friendly format using PHP for the PST timezone.
Here's what I've tried.
date_default_timezone_set('PST');
$this->vars[dateOfEvent] = date('Y-m-d', 1659052800000);
and
date_default_timezone_set('PST');
$dt = new DateTime(1659052800000);
$this->vars[dateOfEvent] = $dt->format('Y-m-d');
but neither give me the correct date. I'm expecting it to out "2022-07-28"
You can set the timezone temporarily for the DateTime object. Also you need to set timestamp divided by 1000 or remove the last three zeroes.
$dt = new DateTime();
$dt->setTimestamp(1659052800000 / 1000);
$dt->setTimezone(new DateTimeZone('PST'));
echo $dt->format('Y-m-d');
prints
2022-07-28

How to convert stored database utc timestamps data into local timezone in PHP

is there any chance to convert database utc zone timestamps record into local timezone in PHP?
//example of code
$utc_date = $row['utc_date'];
// common values
$timezone = "Australia/Melbourne";
$tz = new DateTimeZone($timezone);
$dt = new DateTime();
$dt->setTimezone($tz);
// this can run inside a loop or by itself
$utc_date = $row['utc_date'];
$dt->setTimestamp(strtotime($utc_date));
$datetime = $dt->format("Y-m-d H:i:s");
// use the formatted date as required
echo $datetime;
You can create a new DateTime object inside a loop if you prefer or create it outside the loop and reuse it.
If the date/times don't come out of the database in the format 2019-07-18 10:00:00+00:00 you may have to add +00:00 to the retreived date.
You can simple use following exmaple to resolve your problem
<?php
//Conside in UTC the date and time will be looks like this
$utcTime = time();
echo date("Y-m-d H:i:s", $utcTime);
//now change it to your time zone
date_default_timezone_set('Your-local-time-zone');
$utc_date = date("Y-m-d H:i:s", strtotime($row['utc_date']));
?>
Let Say for my time zone I'll use
date_default_timezone_set('Asia/Kolkata');
Also, You can find your timezone supported by PHP or not here
You can read more about PHP Date funcation from here

PHP Display Date Time with Offset

In php how can I display the current date and time with the timezone offset such as this:
date_default_timezone_set('America/Denver');
$now = new \DateTime('NOW');
echo $now->format("Y-m-d h:i:s");
//
This displays 2019-01-09 19:30:19
but I want it to display 2019-01-09 07:00:00 because I am in Mountain Time Zone
When working with time zones and DateTime() objects you should be using DateTimeZone() instead of date_default_timezone_set()
$now = new \DateTime(null, new DateTimeZone('America/Denver'));
echo $now->format("Y-m-d h:i:s");
Demo
Try to paste second parameter your timezone
$date = new DateTime('NOW', new DateTimeZone('America/Denver));
echo $date->format('Y-m-d h:i:s');
You could define wich timezone is fitting for you by this answer.
List of US Time Zones for PHP to use?
for getting all timezones names in you country. Then you pick your timezone and paste
$date = new DateTime('NOW', new DateTimeZone('Your Country/Your timezone'));
you have right code, but you have want to show time in 12 hours format. then you have use something like that.
date_default_timezone_set('America/Denver');
$now = new \DateTime('NOW');
echo $now->format("Y-m-d h:i:s A");

Convert the UTC datetime to any other time zone dynamically in php

I want to convert the UTC datetime to any other time zone.its working fine.
But it showing only for one time zone,so to view the other time zone need to change the code every time.
i want to convert the UTC datetime to any other time zone dynamically.may be by selecting drop down or some other way.
<?php
$date = new DateTime('2017-11-15 01:00:00 +00');
$date->setTimezone(new DateTimeZone('Asia/Calcutta'));
echo $date->format('Y-m-d H:i:s');
?>
You can timezone by this code where you can change the $timezone variable value from your dropdown.
$timezone= 'Asia/Calcutta';
$date = new DateTime('2017-11-15 01:00:00 +00');
$date = new DateTime($date, new DateTimeZone($timezone));
echo $date->format('Y-m-d H:i:s');

Quicker way to get date of ISO week from a date string?

I'm being passed a date string (most likely in ISO8601 format) and need to convert it to the date of the ISO week to store as a DATETIME column in MySQL. To initialize the DateTime object the I want to save, I'm doing the following:
$date = new DateTime("now");
$date = new DateTime( $date->format("o-\WW") );
echo $date->format(DateTime::ISO8601) . "\n";
Since I'm using Doctrine2, I need to pass my entity a DateTime object. Is there a way to avoid making 2 DateTime objects to get the same result? Should I drop back to the date function and use that as the argument to the DateTime constructor?
$date = new DateTime( date("o-\WW", strtotime("now") );
You could use setISODate to update the first DateTime object using the week and year of the object via format():
$date = new DateTime();
$date->setISODate($date->format('o') , $date->format('W'));
echo $date->format(DateTime::ISO8601);
You could use modify() method of DateTime object.
$date = new DateTime();
$date->modify('sunday this week');
echo $date->format(DateTime::ISO8601) . "\n";
Note that if you want the first day of the week to be something other than Sunday, you will likely need to do something like the following. This example considers Monday as the first day of the week, thus for dates on a Sunday, you would need to get the date of the Monday from the previous week.
$date = new DateTime();
if ($date->format('D') === 'Sun') {
$date->modify('monday last week');
} else {
$date->modify('monday this week');
}
echo $date->format(DateTime::ISO8601) . "\n";
You can probably use date like this:
$date = new DateTime( date('o-\WW') );
Though that formatting looks a bit strange. :p You can of course also use some other method/function the class has to offer to change/modify the date.

Categories