将日期转换为日期时出错

时间:2014-05-01 14:00:52

标签: php jquery mysql sql datetime

我使用jQuery datepicker在输入日期中选择日期。

我在datepicker脚本中使用的格式是:dateFormat: 'DD, d MM, yy',

所以我在输入中得到了这个日期:" Quinta-feira,1 Maio,2014" (葡萄牙语日期)。

但现在我需要将此日期转换为像mysql中的datetime一样保存。

如果日期是英文,我只需要使用下面的代码:

$date = DateTime::createFromFormat('l, j F, Y', $_POST['date']);
echo $date->format('Y-m-d');    

但我的日期不是英文,所以我需要进行转换,我试图使用下面的功能" convertDate"这样做。

但是当我调用该函数时,我传递的输入日期值如下:convertDate($_POST['date']);

我收到错误" 调用此行中的非对象上的成员函数格式():$day= $date->format("l");

你知道这里有什么不对吗?因为这个功能对我来说似乎很好!

function convertDate($myDate){

        $date = DateTime::createFromFormat('Ymd', $myDate);
        $day    = $date->format("l");
        $daynum = $date->format("j");
        $month  = $date->format("F");
        $year   = $date->format("Y");

        switch($day)
        {
            case "Segunda-Feira":    $day = "Monday";  break;
            case "Terça-Feira":      $day = "Tuesday"; break;
            case "Quarta-Feira":     $day = "Wednesday";  break;
            case "Quinta-Feira":     $day = "Thursday"; break;
            case "Sexta-Feira":      $day = "Friday";  break;
            case "Sábado":           $day = "Saturday";  break;
            case "Domingo":          $day = "Sunday";  break;
            default:                 $day = "Unknown"; break;
        }

        switch($month)
        {
            case "Janeiro":    $month = "January";    break;
            case "Fevereiro":  $month = "February";   break;
            case "Março":      $month = "March";     break;
            case "Abril":      $month = "April";     break;
            case "Maio":       $month = "May";       break;
            case "Junho":      $month = "June";      break;
            case "Julho":      $month = "July";      break;
            case "Agosto":     $month = "August";    break;
            case "Setembro":   $month = "September"; break;
            case "Outubro":    $month = "October";   break;
            case "Novembro":   $month = "November";  break;
            case "Dezembro":   $month = "December";  break;
            default:           $month = "Unknown";   break;
        }

    echo $daynum . ", " . $month . ", " . $year;
}

尝试使用str_ireplace:

$english = array("Segunda-Feira","Terça-Feira","Quarta-Feira","Quinta-Feira","Sexta-Feira","Sábado","Domingo");
$portuguese = array("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"); 
$result= str_ireplace ($english , $portuguese, $_POST['date']);
$date = DateTime::createFromFormat('l, j F, Y', $result);
echo $date->format('Y-m-d');

我在echo $date->format('Y-m-d')

中遇到了同样的错误

2 个答案:

答案 0 :(得分:2)

您可以让JQuery在隐藏的表单字段中删除各种类型的UTC,而不是尝试在各种语言中翻译单词中的日期,而是使用它来代替:

这将返回一个JS日期对象:

   var currentDate = $( ".selector" ).datepicker( "getDate" );

这将从中生成PHP UTC(它在JS中的微秒......)

   var phputc = Math.ceil((currentDate.getTime()/1000));

这将以隐藏的形式输入:

   $(".selector" ).change(
        function()
        {
             var currentDate = $( ".selector" ).datepicker( "getDate" );
             var phputc = Math.ceil((currentDate.getTime()/1000));
             document.forms[0].nameofhiddenformfield.value = phputc;
        }
   );

确保您的表单中有这样的字段

   <input type="hidden" name="nameofhiddenformfield" />

然后在PHP方面你可以这样做:

   $dateUTC = $_POST['nameofhiddenformfield'];
   echo date('Y-m-d',$dateUTC);

答案 1 :(得分:1)

您的方法是错误的,在您的函数的第一行中,您尝试将葡萄牙语日期转换为Ymd格式,因此您的$date对象有缺陷。

您应该做什么(假设没有本地化的功能将葡萄牙语日期转换为DateTime ...),使用类似字符串替换的内容替换所有葡萄牙语单词和英语单词,然后使用原始转换:

$date = DateTime::createFromFormat('l, j F, Y', $translated_date);

有关的17/34个单词不会相互干扰,因此str_ireplace()有两个数组应该这样做。