Jump to content

Date & Time Formating


stephenl7

Recommended Posts

Hello

 

I am trying to store the date and time from a email header (retrived using imap_fetch_overview) in a MYSql database

 

My current code seems logical, however doesn't work

 

    // read datetime from header

    $datestr = $overview->date;

    // format
    $dateTime = DateTime::createFromFormat('D, d M Y H:i:s O', $datestr);

    $errors = DateTime::getLastErrors();
    if (!empty($errors['warnings'])) {
    echo "Invalid Date\n"; }

 

    // convert to mysql format   
    $date = $dateTime->format('Y-m-d H:m:s');

 

Reported error

Fatal error: Call to a member function format() on a non-object in

 

Any advise and suggestions would be appreciated

 

Thank you

 

Link to comment
https://forums.phpfreaks.com/topic/282152-date-time-formating/
Share on other sites

Your object creation is failing, returning a boolean false.  Make sure your date string matches your format string.

 

Right now you are telling it to create the string from a date that looks like "Mon, 01 Jan 2013 23:40:12 +0500".

 

I would suggest dumping the $datestr variable, and making sure it matches that format.

Link to comment
https://forums.phpfreaks.com/topic/282152-date-time-formating/#findComment-1449474
Share on other sites

To convert a date retrieved from MySQL into the format you requested (mm/dd/yy H:M (AM/PM)), try the following;

$datetime = strtotime($row->createdate);
$mysqldate = date("m/d/y g:i A", $datetime);

 

MySQL's standard format is: date("Y-m-d H:i:s", $datetime)

Link to comment
https://forums.phpfreaks.com/topic/282152-date-time-formating/#findComment-1449678
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.