使用PHP日期函数进行对话时出错

时间:2017-12-04 16:41:01

标签: php date

我在使用php date函数转换日期字段值时遇到了错误的一年。我在下面提供我的代码。

usort($result, function($a, $b) {
            $ad = DateTime::createFromFormat('m/d/Y H:i:s A', $a['date'])->format('m/d/Y H:i:s A');
            $bd = DateTime::createFromFormat('m/d/Y H:i:s A', $b['date'])->format('m/d/Y H:i:s A');
                          if ($ad == $bd) {
                            return 0;
 }
     return $ad > $bd ? -1 : 1;
});
echo json_encode($dataArr);

这是给出以下输出。

[{"message":"HAPPY HOUR has removed from the business Royalcausine","image":"","rest_name":"Royalcausine","member_id":"254","subcat_id":"42","subcat_name":"HAPPY HOUR","day_id":"1","day_name":"Monday","city_id":"2","city_name":"Bhubaneswar","noti_id":"76","special_id":"1","date":"12/04/2017 20:50:52 PM"},{"message":"FREE TODAY TECH 1212","image":"","rest_name":"Swagat restaurant","member_id":"244","subcat_id":"42","subcat_name":"HAPPY HOUR","day_id":"1","day_name":"Monday","city_id":"2","city_name":"Bhubaneswar","noti_id":"84","special_id":"1","date":"12/04/2017 15:31:57 PM"},{"message":"happy hour 90% discount updated by subhra","image":"","rest_name":"1st choice","member_id":"189","subcat_id":"42","subcat_name":"HAPPY HOUR","day_id":"4","day_name":"Thursday","city_id":"2","city_name":"Bhubaneswar","noti_id":"68","special_id":"1","date":"12/01/2017 11:35:52 AM"},{"message":"Why to wait?grab now","image":"","rest_name":"kolaberi","member_id":"245","subcat_id":"57","subcat_name":"COCKTAILS","day_id":"4","day_name":"Thursday","city_id":"2","city_name":"Bhubaneswar","noti_id":"67","special_id":"1","date":"11/30/2017 14:23:10 PM"},{"message":"Great offers now 23%","image":"","rest_name":"kk","member_id":"241","subcat_id":"47","subcat_name":"LATE NIGHT","day_id":"2","day_name":"Tuesday","city_id":"2","city_name":"Bhubaneswar","noti_id":"6","special_id":"1","date":"11/28/2017 17:33:47 PM"},{"message":"90% discount","image":"","rest_name":"Swagat restaurant","member_id":"244","subcat_id":"42","subcat_name":"HAPPY HOUR","day_id":"2","day_name":"Tuesday","city_id":"2","city_name":"Bhubaneswar","noti_id":"5","special_id":"1","date":"11/28/2017 17:08:33 PM"}]

我再次转换日期字段值,如下所示。

foreach ($result as $key => $value) {
    $result[$key]['date']=date("jS F, Y h:i:s A", strtotime($value['date']));
}

最后它给出了如下输出。

[{"message":"HAPPY HOUR has removed from the business Royalcausine","image":"","rest_name":"Royalcausine","member_id":"254","subcat_id":"42","subcat_name":"HAPPY HOUR","day_id":"1","day_name":"Monday","city_id":"2","city_name":"Bhubaneswar","noti_id":"76","special_id":"1","date":"1st January, 1970 01:00:00 AM"},{"message":"FREE TODAY TECH 1212","image":"","rest_name":"Swagat restaurant","member_id":"244","subcat_id":"42","subcat_name":"HAPPY HOUR","day_id":"1","day_name":"Monday","city_id":"2","city_name":"Bhubaneswar","noti_id":"84","special_id":"1","date":"1st January, 1970 01:00:00 AM"},{"message":"happy hour 90% discount updated by subhra","image":"","rest_name":"1st choice","member_id":"189","subcat_id":"42","subcat_name":"HAPPY HOUR","day_id":"4","day_name":"Thursday","city_id":"2","city_name":"Bhubaneswar","noti_id":"68","special_id":"1","date":"1st December, 2017 11:35:52 AM"},{"message":"Why to wait?grab now","image":"","rest_name":"kolaberi","member_id":"245","subcat_id":"57","subcat_name":"COCKTAILS","day_id":"4","day_name":"Thursday","city_id":"2","city_name":"Bhubaneswar","noti_id":"67","special_id":"1","date":"1st January, 1970 01:00:00 AM"},{"message":"Great offers now 23%","image":"","rest_name":"kk","member_id":"241","subcat_id":"47","subcat_name":"LATE NIGHT","day_id":"2","day_name":"Tuesday","city_id":"2","city_name":"Bhubaneswar","noti_id":"6","special_id":"1","date":"1st January, 1970 01:00:00 AM"},{"message":"90% discount","image":"","rest_name":"Swagat restaurant","member_id":"244","subcat_id":"42","subcat_name":"HAPPY HOUR","day_id":"2","day_name":"Tuesday","city_id":"2","city_name":"Bhubaneswar","noti_id":"5","special_id":"1","date":"1st January, 1970 01:00:00 AM"}]

在这里,我注意到2017年正在转换为1970这是一个错误的值。最后,我需要像28th Nov, 2017 17:08:33 PM这样的日期格式。

0 个答案:

没有答案