Why can't I access DateTime-date in PHP's DateTime class?
This is a known issue.
Date being available is actually a side-effect of support for
var_dump()
here – derick@php.net
For some reason, you're not supposed to be able to access the property but var_dump
shows it anyways. If you really want to get the date in that format, use the DateTime::format()
function.
echo $mydate->format('Y-m-d H:i:s');
How to get current time in Datetime format PHP?
You should find info about the DateTime class to work with dates in php
$d = new DateTime('now');
$d->setTimezone(new DateTimeZone('UTC'));
echo $d->format('Y-m-d H:i:s');
Object of class DateTime could not be converted
You can't echo
the DateTime
object directly. You have to use the format
method to get the date and / or time part:
$tempDate = DateTime::createFromFormat('j-M-Y', '15-Feb-2009');
echo $tempDate->format('Y-m-d H:i:s');
// NOT echo $tempDate!!!
demo: http://ideone.com/IyqRWj
If you want to see the details of the object (for debug) you can use var_dump
:
$tempDate = DateTime::createFromFormat('j-M-Y', '15-Feb-2009');
var_dump($tempDate);
DateTime object returning NULL without var_dump
It has something to do with PHP being forced to create a full object representation when you dump the whole object which happens to update the object state, as opposed to you just accessing the one individual property.
The bigger point is though that you don't really have any business accessing DateTime::$date
at all. It's an undocumented internal implementation detail that you're not supposed to use anywhere. If you want to output the date, use DateTime::format
or one of the other public interface methods.
Verify valid date using PHP's DateTime class
You can try this one:
static public function verifyDate($date)
{
return (DateTime::createFromFormat('m/d/Y', $date) !== false);
}
This outputs true/false. You could return DateTime
object directly:
static public function verifyDate($date)
{
return DateTime::createFromFormat('m/d/Y', $date);
}
Then you get back a DateTime
object or false on failure.
UPDATE:
Thanks to Elvis Ciotti who showed that createFromFormat accepts invalid dates like 45/45/2014.
More information on that: https://stackoverflow.com/a/10120725/1948627
I've extended the method with a strict check option:
static public function verifyDate($date, $strict = true)
{
$dateTime = DateTime::createFromFormat('m/d/Y', $date);
if ($strict) {
$errors = DateTime::getLastErrors();
if (!empty($errors['warning_count'])) {
return false;
}
}
return $dateTime !== false;
}
DateTime function affects original variable when modifying a new variable
$new = $original;
just assigns the same reference that's in $original
to $new
. Instead you want to make a copy.
$new = clone $original;
PHP Issues with DateTime in array from JIRA api
$issue->fields->created
is already a DateTime
object. To format that as Y-m-d
format, you can simply call format()
on that directly:
echo '<tr>
<td>'.$issue->fields->status->name.'</td>
<td>'.$issue->fields->created->format('Y-m-d').'</td>
<td>'.$issue->fields->priority->name.'</td>
<td>'.$issue->fields->summary.'</td>
<td>'.$issue->fields->assignee->displayName.'</td>
</tr>';
If you want to convert it to a different timezone, you can do that as well:
$issue->fields->created->setTimezone(new DateTimeZone("UTC"))->format('Y-m-d')
Related Topics
Replace Urls in Text With HTML Links
Undefined Function MySQL_Connect()
Merge Two Arrays as Key Value Pairs in PHP
How to Create Friendly Urls With .Htaccess
Truncate a String to First N Characters of a String and Add Three Dots If Any Characters Are Removed
How to Read Any Request Header in PHP
Understanding MVC Views in PHP
The Plugin Generated X Characters of Unexpected Output During Activation (Wordpress)
How to Use Password Hashing With Pdo to Make My Code More Secure
Slowness Found When Base 64 Image Select and Encode from Database
How to [Recursively] Zip a Directory in PHP
Unicode Character in PHP String
Is There a PHP Function That Can Escape Regex Patterns Before They Are Applied