I am getting the following date format from a JSON response and want to format it better, but I am a little unsure how to.
Current Response: Wed Mar 02 03:00:00 +1100 2016
Required Response: 2nd of March 2016
Current PHP for output:
$purchase_data['verify-purchase']['supported_until']
some speacial formatting here, but possible when reading the manual. also take care of time zone…
<?php
//custom function
function reformatDate( $old, $correction ) {
// makes it a number of seconds since 1970…
$old_date_timestamp = strtotime( $old );
//formats again as string
return date( 'jS F Y', $old_date_timestamp + $correction );
}
//Input: Wed Mar 02 03:00:00 +1100 2016
//timezone needs to be taken care of
print reformatDate(
"Wed Mar 02 03:00:00 +1100 2016", //here you put your input variable
11*60*60 // here 11h, but maybe the difference of timezones needs to be changed – only you will know after edge cases ;)
);
//desired output: 2nd of March 2016 – check
?>
to do it even better you could ask your own timezone from the local setting. date can help you there as well, or you go by timezone_offset_get to automate that … the latter is more tricky as it raises an error when not set before.
Try this out:
$old_date_timestamp = strtotime($purchase_data['verify-purchase']['supported_until']);
$new_date = date('d F Y', $old_date_timestamp);
print $new_date;
Someone posted an answer on here but for some reason was deleted, however i used some of their solution and adapted and created into a function so for anyone else needing to handle a returned date formatted in such a way here is what i done.
function dateFormat($date){
$current_date = $date;
return date("jS", strtotime($current_date) ) . ' of ' . date("F Y", strtotime($current_date) );
}
Then called it like so....
dateFormat($purchase_data['verify-purchase']['supported_until']);
Output went from:
Wed Mar 02 03:00:00 +1100 2016
To:
1st of March 2016
Actually noticed now writing this, it is rounding off to 1st rather than 2nd?
UPDATED
Updated with answer from @vv01f to correct the date, here is the final result
function dateFormat( $old, $correction ) {
$old_date_timestamp = strtotime( $old );
return date( 'jS F Y', $old_date_timestamp + $correction );
}
Calling it like so...
dateFormat($purchase_data['verify-purchase']['supported_until'], 11*60*60);