4

I have a string of a date:

25/08/2012

And here I'm trying to convert it to a DateTime object in order to save it to the MySQL database. My backend schema has a DateOfPrint date column ready to receive this data.

Here's what I've tried:

$eventDate = DateTime::createFromFormat('d/m/y', $fecha->innertext);
echo $eventDate;

The echo statement doesn't show anything on the screen, and when trying to save it to the database, nothing is saved in that column.

Any suggestions?

5 Answers 5

11

Your $eventDate contains a boolean(false) which is printed as empty string.

You need to use an upper-case Y.

Y   A full numeric representation of a year, 4 digits    Examples: 1999 or 2003
y   A two digit representation of a year    Examples: 99 or 03

And you have to call DateTime::format();
e.g.

<?php
$fecha = new StdClass;
$fecha->innertext = '25/08/2012';

$eventDate = DateTime::createFromFormat('d/m/Y', $fecha->innertext);
if ( false===$eventDate ) {
  die('invalid date format');
}
echo $eventDate->format('Y-m-d');

prints

2012-08-25
Sign up to request clarification or add additional context in comments.

3 Comments

Your answer says I should use format() but your example is using createFromFormat() Which one should I use?
@Serg - You use createFromFormat() to get a DateTime object, and format() to correctly format that date object to a string that is acceptable for a MySQL DATE column.
Both. createFromFormat() for parsing the string into a datetime objec. And format() to get a (different) string representation back from that object.
3

You need to format it for a MySQL column before you can insert it:

// Need upper case Y here, thanks to VolkerK for pointing that out
$eventDate = DateTime::createFromFormat('d/m/Y', $fecha->innertext);
$eventDate = $eventDate->format( 'Y-m-d'); // I think this is the correct format

Then you can use $eventDate to save the date to the database.

2 Comments

I love your work with this class. You get my 40th +1 for today!
@Fluffeh - Hah, thanks. It's really a great hidden gem that many don't know about, and it's so useful!
1

$eventDate is an object, not a string. You will need to access the properties of the element in your code to be able to correctly insert it's value into a table or echo it out. On that note, you could use a var_dump($eventDate); which should show you all there is to know about the object.

You can reference the PHP docsm on the DateTime class to get the available properties and see which one best fits your needs.

Comments

0
$eventDate = DateTime::createFromFormat('d/m/Y', $fecha->innertext);
echo $eventDate->format('Y-m-d');

Comments

0

short answer

$st_time =  date('Y-m-d H:i',$yourdate);

if you want only day month and year use this

$st_time =  date('Y-m-d',$yourdate);

1 Comment

Although your example might work, the OP is specifically asking with regard to the DateTime class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.