Jump to content

[SOLVED] PHP Dates


sseeley

Recommended Posts

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

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

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

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

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.