PHP date adding in DateTime object does not work - php

Try to do:
$datetime = new DateTime();
$onehour = new DateInterval('PT1H');
$datetime->add($onehour);
$timestamp = $datetime->date;
echo $timestamp; // nothing
What's wrong with it?

There's no such date property in the DateTime object.
Instead you can use format() to get the date formatted according to given format.
echo $datetime->format('Y-m-d H:i:s');
or use getTimestamp() to get the Unix timestamp:
echo $datetime->getTimestamp();
http://php.net/manual/en/datetime.format.php
http://php.net/manual/en/datetime.gettimestamp.php

You have to use :
DateInterval::createFromDateString("1 hours");
Instead of :
new DateInterval('PT1H');
As Given Below Code :
$onehour = DateInterval::createFromDateString("1 hours");
$date = new DateTime();
$date->add($onehour);
echo $date->format("Y-m-d H:i:s");

Related

Adding 30 days to DateTime with TimeZone is resulting to an error: "Call to a member function modify() on string"

I am trying to add 30 days to the current day using the php modify() function and I also am putting timezone in the datetime here is my code:
$country_code = 'PH';
$timezone = \DateTimeZone::listIdentifiers(\DateTimeZone::PER_COUNTRY, $country_code);
$new_date = (new \DateTime("now", new \DateTimeZone($timezone[0])))->format('Y-m-d H:i:s');
$datetime = $new_date->modify('+30 days');
$end_date = $datetime->format('Y-m-d H:i:s');
I am getting the error:
Call to a member function modify() on string
When I echo the $new_date it is in proper MySQL datetime format 'Y-m-d H:i:s'. What am I doing wrong?
Remove the ->format('Y-m-d H:i:s') as that creates $new_date as a string and not a DateTime Object that has a modify() method on it
$country_code = 'PH';
$timezone = \DateTimeZone::listIdentifiers(\DateTimeZone::PER_COUNTRY, $country_code);
$new_date = (new \DateTime("now", new \DateTimeZone($timezone[0])));
$new_date->modify('+30 days');
$end_date = $new_date->format('Y-m-d H:i:s');
Or you could do it all in one line
$new_date = (new \DateTime("now", new \DateTimeZone($timezone[0])))->modify('+30 days')->format('Y-m-d H:i:s');
ok this is crazy, I played with it and came up with a 1 liner solution..
$end_date = (new \DateTime("now", new \DateTimeZone($timezone[0])))->modify('+30 days')->format('Y-m-d H:i:s');
Apparently it should be formatted after being modified but in my earlier code it is being formatted first before modified then formatted once again.

Convert timezone from name to UTC

Sorry but I still don't know how to write a good title for this question.
In database, I have a string of timezone, ex: "Asia/Bangkok" and I want to convert it to "+07:00". How I can do it?
Here is my code:
$newTZ = new DateTimeZone("Asia/Bangkok");
But I don't know what is next. Thanks you so much.
Simply set the timezone to a DateTime instance and display using the "P" format
$newTZ = new DateTimeZone("Asia/Bangkok");
echo (new DateTime('now', $newTZ))->format('P'); // displays "+07:00" for 'now'
https://3v4l.org/0Cb0C
//GMT
$time = '01/01/2018 12:00 AM';
$zone = 'Asia/Bangkok';
$schedule_date = new DateTime($time, new DateTimeZone($zone) );
$schedule_date->setTimeZone(new DateTimeZone('UTC'));
$time2 = $schedule_date->format('Y-m-d H:i:s');
//Time conveted to TimeZone
echo $time2;
//Compare time between two dates
$date1=date_create($time);
$date2=date_create($time2);
echo "<pre>";
$object = date_diff($date1, $date2);
//Detailed object
print_r($object);
//Get whatever format you want.
echo $object->h . ':' . $object->i;

How to calculate the difference of datetime field and now in Cakephp?

I have a datetime field in my database that contains the following information:
2015-08-04 18:59:01
I want to check the difference between that datetime field and now using Cakephp framework ?
See DateTime::diff
$date = '2015-08-04 18:59:01';
$dateTime = new DateTime($date);
$now = new DateTime();
$interval = $now->diff($dateTime);
echo $interval->format('%R%a days');
See DateInterval::format for other formatting options.
You can also get diff in seconds:
$date = '2015-08-04 18:59:01';
$dateTime = new DateTime($date);
$diff = time() - $dateTime->getTimestamp();
Calculate the difference between two dates:
$date1=date_create("2013-03-15");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);
echo $diff->format("%R%a days");
Output:
+272 days
The date_diff() function returns the difference between two DateTime objects.

How to convert negative timestamp to date (PHP)

I want to convert negative timestamp value to human readable value. This is my code:
$timestamp = -30607469951;
$date = new DateTime();
$date->setTimestamp($timestamp);
$date->format('j/n/Y');
All i did was echo out the formatted $date, is that what you wanted?
$timestamp = -30607469951;
$date = new DateTime();
$date->setTimestamp($timestamp);
echo $date->format('Y-m-d');

strtotime with variable using php - object given

This is my current code:
$dateGame = new DateTime();
date_modify($dateGame, "+$universeTime Year");
$arrivalTime = date('Y-M-d H:i:s', strtotime("+$flightTimeMin minutes", $dateGame));
This isn't working because I believe "$dateGame" is an object. How would I turn it into something readable by "strtotime"?
Thanks
You dont need to use strtotime() since you are using datetime and you can use datetime object to format the date as
$universeTime = 3 ;
$flightTimeMin = 20;
$dateGame = new DateTime();
date_modify($dateGame, "+$universeTime Year +$flightTimeMin minutes");
If you want to format the display you can use as
echo $dateGame->format('Y-m-d H:i:s');
or just use
echo $dateGame->date ;

Categories