0

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
satya
  • 3,508
  • 11
  • 50
  • 130
  • Can you reduce the code to a minimal working example and share your inputs? Probably, converting the date does not work as expected as the inputted date has the wrong format? – Nico Haase Dec 04 '17 at 16:43
  • Possible duplicate of [strtotime was not working in php with m-d-Y format in php?](https://stackoverflow.com/questions/44069143/strtotime-was-not-working-in-php-with-m-d-y-format-in-php) – Andreas Dec 04 '17 at 16:47
  • Try converting the time in your first loop to a more standard, non-ambiguous format, like `YYYY-MM-DD H:i:s`. Your second loop will run a lot easier. – aynber Dec 04 '17 at 16:50
  • @aynber: Can you resolve this ? – satya Dec 04 '17 at 16:56
  • `->format('m/d/Y H:i:s A');` change to `->format('Y-m-d H:i:s');` in your `usort` – aynber Dec 04 '17 at 17:08
  • @aynber: I did as per you but same issue. – satya Dec 04 '17 at 17:10
  • Try echoing/logging the existing date and the converted date in your foreach loop to see what the values are. – aynber Dec 04 '17 at 17:20
  • @aynber: my existing date is like `12/04/2017 20:50:52 PM` and its converted date is `1st January, 1970 01:00:00 AM`. – satya Dec 04 '17 at 17:23
  • Oh, right, it won't pass from `usort`. I'd suggest you use `DateTime` in the foreach exactly like you're doing in the `usort`. – aynber Dec 04 '17 at 17:25
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/160449/discussion-between-satya-and-aynber). – satya Dec 04 '17 at 17:31

0 Answers0