Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJulaine Scott
38,411 PointsCannot find correct format for date
I have tried to answer this question many different ways. The code that should work is
$timestamp = strtotime($date);
if ($timestamp === false) {
return date("YYYY-MM-DD",$date);
} else {
$cleanDate = date('YYYY-MM-DD',strtotime($timestamp));
return $cleanDate;
}
But I keep getting an error message 'Does not return the correct format.' What have I missed?
<?php
function convert_date_sql($date) {
//add your code here
$timestamp = strtotime($date);
if ($timestamp === false) {
return date("YYYY-MM-DD",$date);
} else {
$cleanDate = date('YYYY-MM-DD',strtotime($timestamp));
return $cleanDate;
}
}
2 Answers
Joel Bardsley
31,249 PointsHi Julaine, I think you're over-complicating this one. As strtotime() returns either an int timestamp on success or false on failure, you don't really need the conditional. Assume the timestamp is false and you pass it into the date function like date('F jS Y', $timestamp) - it would return a clean date of January 1st 1970 in the format specified.
Speaking of date formats, this is where you appear to be going wrong. I would recommend refreshing yourself with the date() documentation and check what characters represent a 4-digit year and so on. Note how I've formatted the date in my example above and see how it can be modified to show YYYY-MM-DD / 1970-01-01 instead.
If you need further assistance, please let me know. Good luck!
Julaine Scott
38,411 PointsThank you, I figured it must be something obvious.