ScriberUK
asked on
Echo PHP DateTime Object Element
Below in a simple DateTime Object, I would like to echo the date
But for some reason it doesn't work???
Help!
2010-09-01 00:00:00by using something:
echo gmdate($row['WHENTORUNNEXT']->date);
But for some reason it doesn't work???
Help!
Array
(
[HHMMOFDAYTORUN] => 15
[WHENTORUNNEXT] => DateTime Object
(
[date] => 2010-09-01 00:00:00
[timezone_type] => 3
[timezone] => Europe/London
)
)
use var_dump() to print the $row variable and post that here, please.
$ts = strtotime($row['WHENTORUNN EXT']->dat e);
echo date('d/m/Y', $ts);
echo date('d/m/Y', $ts);
Please see the man page here:
http://us.php.net/manual/en/function.gmdate.php
gmdate() expects a timestamp and a format statement. The timestamp is what strtotime() will do for you. I have an article on DATETIME handling here at EE that may be helpful to you.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html
http://us.php.net/manual/en/function.gmdate.php
gmdate() expects a timestamp and a format statement. The timestamp is what strtotime() will do for you. I have an article on DATETIME handling here at EE that may be helpful to you.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html
ASKER
Very close spsenthil!
echo date('Y-m-d h:i:s', $ts); prints "1970-01-01 01:00:00" but it should print "2010-09-01 00:00:00"?
Below is the var_dump()
Thanks!
echo date('Y-m-d h:i:s', $ts); prints "1970-01-01 01:00:00" but it should print "2010-09-01 00:00:00"?
Below is the var_dump()
Thanks!
array(2) {
["HHMMOFDAYTORUN"]=>
int(15)
["WHENTORUNNEXT"]=>
object(DateTime)#19 (3) {
["date"]=>
string(19) "2010-09-01 00:00:00"
["timezone_type"]=>
int(3)
["timezone"]=>
string(13) "Europe/London"
}
}
ASKER
This works:
echo gmdate('Y-m-d H:i:s', strtotime('2010-09-01 00:00:00'));
Giving: 2010-08-31 23:00:00
But this doesn't:
echo gmdate('Y-m-d H:i:s', strtotime($row['WHENTORUNN EXT']->dat e));
Giving: 1970-01-01 00:00:00
echo gmdate('Y-m-d H:i:s', strtotime('2010-09-01 00:00:00'));
Giving: 2010-08-31 23:00:00
But this doesn't:
echo gmdate('Y-m-d H:i:s', strtotime($row['WHENTORUNN
Giving: 1970-01-01 00:00:00
"strtotime('2010-09-01 00:00:00')" is intended for converting text expressions like Tuesday, September, etc. to time. It doesn't and isn't intended to convert a numeric expression like that.
And if you get"1970-01-01 00:00:00" or "1969-12-31 00:00:00", then you gave 'gmdate' a 0 or a null. That value is the begining of the Unix Epoch and represents a Unix timestamp of 0.
And if you get"1970-01-01 00:00:00" or "1969-12-31 00:00:00", then you gave 'gmdate' a 0 or a null. That value is the begining of the Unix Epoch and represents a Unix timestamp of 0.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
strtotime() will return FALSE on failure. When that happens you can print out the variable that caused the failure.
Example:
$timestamp = strtotime($string);
if ($timestamp === FALSE) echo "$string is bogus";
Example:
$timestamp = strtotime($string);
if ($timestamp === FALSE) echo "$string is bogus";
This produced the expected output. Not sure why var_dump() shows me nothing?
<?php // RAY_temp_thing.php
error_reporting(E_ALL);
echo "<pre>";
/* FROM THE POST AT EE
Array
(
[HHMMOFDAYTORUN] => 15
[WHENTORUNNEXT] => DateTime Object
(
[date] => 2010-09-01 00:00:00
[timezone_type] => 3
[timezone] => Europe/London
)
)
*/
// MAKE A DATETIME OBJECT AND LOAD IT UP
$d = new DateTime();
$d->setDate(2010, 9, 1);
echo $d->format('Y-m-d');
// WONDER WHY THIS DOES NOT TELL US MORE?
var_dump($d);
Ray,
Yeah, that baffled me too? Didn't seem to matter whether you var_dump or print_r the DateTime object - it doesn't show any info.
The class definition doesn't seem to expose any properties - only methods. Had a feeling this might be down to the version of PHP I'm running (5.2.6) but didn't have time to test further!
It seems the only way to get the data back out of the object is to called the various methods - format(), getTimezone(), getTimestamp() etc.
http://www.php.net/manual/en/class.datetime.php
Yeah, that baffled me too? Didn't seem to matter whether you var_dump or print_r the DateTime object - it doesn't show any info.
The class definition doesn't seem to expose any properties - only methods. Had a feeling this might be down to the version of PHP I'm running (5.2.6) but didn't have time to test further!
It seems the only way to get the data back out of the object is to called the various methods - format(), getTimezone(), getTimestamp() etc.
http://www.php.net/manual/en/class.datetime.php
ASKER
Thank you very much for your help guys (and gals)! I have been away on business and haven't had a chance to check yet but it is just very nice to know I'm not doing something silly :)
The clean and simple solution is, as ChrisStanyon posted:
echo $row['WHENTORUNNEXT']->format("Y-m-d H:i:s");
<?php
$row['WHENTORUNNEXT']->dat e = "2010-09-01 00:00:00";
$full_date = $row['WHENTORUNNEXT']->dat e;
$explode = explode(" ", $full_date);
$date_part = explode("/", $explode[0]);
$unix_ts = mktime(0,0,0, $date_part[2], $date_part[2], $date_part[3]);
echo gmdate("M d Y", $unix_ts);
?>
$row['WHENTORUNNEXT']->dat
$full_date = $row['WHENTORUNNEXT']->dat
$explode = explode(" ", $full_date);
$date_part = explode("/", $explode[0]);
$unix_ts = mktime(0,0,0, $date_part[2], $date_part[2], $date_part[3]);
echo gmdate("M d Y", $unix_ts);
?>
This question has been classified as abandoned and is being closed as part of the Cleanup Program. See my comment at the end of the question for more details.