I am getting some wrong year while converting date field value using php date
function. I am providing my code below.
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);
This is giving the below output.
[{"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"}]
again I am converting the date field value like below.
foreach ($result as $key => $value) {
$result[$key]['date']=date("jS F, Y h:i:s A", strtotime($value['date']));
}
And finally it's giving the output like below.
[{"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"}]
Here I noticed the year 2017
is converting into 1970
which is a wrong value. Finally I need this date format like 28th Nov, 2017 17:08:33 PM
.