Question:
PHP date formating?
jeff lemon
2007-08-21 09:37:29 UTC
The date field is input in a field on my form as:

DD/MM/YYYY

But I need it formatting before I submit t to the database using php. The format I need is YYYY-MM-DD

How can I do this?
Five answers:
sheepbalz
2007-08-22 09:01:58 UTC
Much simpler than the above posts...



date('Y-m-d', strtotime($_POST['submitdate'] ) );



what this does is converts the date submitted on your form to a timestamp which the date function can use to format it however it needs to be.
Big D
2007-08-21 16:58:42 UTC
Above is right if you need the current date, if this is the case you could also just do a NOW() or format the now for what you need DATE_FORMAT(NOW(), '%Y-%m-%d') (which is what I would go with so that you don't need to validate information or do alot of unnecessary functions.



if not

$inputtedDate = $_GET['inputtedDate'];

$year = substr($inputtedDate, 6); //gets the character indexed at 6 and beyond

$month = substr($inputtedDate, 3, 2); //gets the 2 characters starting with the index of 3

$day = substr($inputtedDate, 0, 2); //get the 2 characters indexed at 0

$formattedDate = $year . '-' . $month . '-' . $day;
?
2007-08-21 17:10:24 UTC
Go back to college, man! You have asked over 20 questions here, all of which you should know if you were not so lazy!
Doyzer
2007-08-21 16:53:05 UTC
$dbDate = date('Y-m-d', $formDate)




// Assuming today is: March 10th, 2001, 5:16:18 pm



$today = date("F j, Y, g:i a"); // March 10, 2001, 5:16 pm

$today = date("m.d.y"); // 03.10.01

$today = date("j, n, Y"); // 10, 3, 2001

$today = date("Ymd"); // 20010310

$today = date('h-i-s, j-m-y, it is w Day z '); // 05-16-17, 10-03-01, 1631 1618 6 Fripm01

$today = date('\i\t \i\s \t\h\e jS \d\a\y.'); // It is the 10th day.

$today = date("D M j G:i:s T Y"); // Sat Mar 10 15:16:08 MST 2001

$today = date('H:m:s \m \i\s\ \m\o\n\t\h'); // 17:03:17 m is month

$today = date("H:i:s"); // 17:16:17

?>
Terence P
2007-08-21 16:41:27 UTC

date('Y-m-d');

?>


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...