sseeley Posted March 7, 2009 Share Posted March 7, 2009 Hi I am hoping someone can help me...a user fills in their date of birth for example, 22-01-2005 This is then posted to another form...as shown in the code $date_of_birth = $_POST['date_of_birth']; echo $date_of_birth; $date_of_birth_mysql = date("Y-m-d",strtotime($date_of_birth)); echo $date_of_birth_mysql; When it is then converted to a mysql date it appears as follows? These are the two strings echoed... 22-01-2005 2027-06-28 Can anyone help as to why this code is doing this? Many thanks Link to comment https://forums.phpfreaks.com/topic/148378-solved-php-dates/ Share on other sites More sharing options...
v3nom Posted March 7, 2009 Share Posted March 7, 2009 What exactly are you trying to do? If you want to put that date into words, then strtotime isn't the function to use. "strtotime" parses about any English textual datetime description into a Unix timestamp. Link to comment https://forums.phpfreaks.com/topic/148378-solved-php-dates/#findComment-779022 Share on other sites More sharing options...
PFMaBiSmAd Posted March 7, 2009 Share Posted March 7, 2009 These are the formats that strtotime understands - http://www.gnu.org/software/tar/manual/html_node/Calendar-date-items.html#SEC116 dd-mm-yyyy is not a format that you can use with strtotime. Link to comment https://forums.phpfreaks.com/topic/148378-solved-php-dates/#findComment-779024 Share on other sites More sharing options...
sseeley Posted March 7, 2009 Author Share Posted March 7, 2009 I want to convert a standard date into a MYSQL date... Link to comment https://forums.phpfreaks.com/topic/148378-solved-php-dates/#findComment-779040 Share on other sites More sharing options...
daanoz Posted March 7, 2009 Share Posted March 7, 2009 A simple solution (which returns an unix stamp): public function ParseDate($Stamp) { $aDateParts = explode("-", $Stamp); if(count($aDateParts) != 3) { return 0; } else { return mktime(0, 0, 0, $aDateParts[1], $aDateParts[0], $aDateParts[2]); } } or to mysqlstamp public function ParseDateToMysql($Stamp) { $aDateParts = explode("-", $Stamp); if(count($aDateParts) != 3) { return "0000-00-00 00:00:00"; } else { return date("Y-m-d H:i:s",mktime(0, 0, 0, $aDateParts[1], $aDateParts[0], $aDateParts[2])); } } Link to comment https://forums.phpfreaks.com/topic/148378-solved-php-dates/#findComment-779042 Share on other sites More sharing options...
sseeley Posted March 7, 2009 Author Share Posted March 7, 2009 Thanks, but I cannot get this to work, I tried the following... With a 'public function' I get this error Parse error: syntax error, unexpected T_PUBLIC in C:\server\www\myserver.dev\public_html\accounts\test.php on line 3 If I remove the 'public' nothing returns at all? <?php $aDateParts = "01-04-1977"; function ParseDateToMysql($Stamp) { $aDateParts = explode("-", $Stamp); if(count($aDateParts) != 3) { return "0000-00-00 00:00:00"; } else { return date("Y-m-d H:i:s",mktime(0, 0, 0, $aDateParts[1], $aDateParts[0], $aDateParts[2])); } } ?> Would someone be able to explain further how this works, sorry really new at this PHP... Link to comment https://forums.phpfreaks.com/topic/148378-solved-php-dates/#findComment-779049 Share on other sites More sharing options...
daanoz Posted March 7, 2009 Share Posted March 7, 2009 right, sorry, that is when your work Object oriented.. try this: <?php $aDateParts = "01-04-1977"; function ParseDateToMysql($Stamp) { $aDateParts = explode("-", $Stamp); if(count($aDateParts) != 3) { return "0000-00-00 00:00:00"; } else { return date("Y-m-d H:i:s",mktime(0, 0, 0, $aDateParts[1], $aDateParts[0], $aDateParts[2])); } } echo ParseDateToMysql($aDateParts); ?> Link to comment https://forums.phpfreaks.com/topic/148378-solved-php-dates/#findComment-779107 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.