Converting date from JSON response in PHP - php

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);

Related

PHP : reformat a date in

Good day,
I have the following date that I get from my imap_fetch_overview PHP function :
$overview = imap_fetch_overview($inbox, $email_number, 0);
...
$date_event = $overview[0]->date;
This outputs a date like 'Fri, 30 Jun 2017 16:27:44 +0000 (UTC)'
In the meantime I have set my default timezone as following :
date_default_timezone_set('Europe/Brussels');
What I would like now is to be able to retrieve my $date_event in a format dd.mm.yyyy HH:mm:ss (local time, in my case 30.06.2017 18:27:44).
I have therefore tried :
$date_event_formated = $date_event('dd.mm.YYYY HH:ii:ss');
When calling 'echo $date_event_formated;', the only thing I get is a Fatal error.
So sorry if the question might sound silly, but I don't really understand what I am doing wrong here? Before bothering you, I looked at the reference website http://php.net/manual/fr/function.date.php but I may have missed something.
Thanks very much for your time and appreciated help.
While there is nothing wrong with Jakub's answer, using php 5.3 and up you can also use DateTime::createFromFormat, using an exact mask to parse the incoming date-string.
$oldDate = 'Fri, 30 Jun 2017 16:27:44 +0000 (UTC)';
$date = DateTime::createFromFormat('D, d M Y H:i:s T e', $oldDate);
echo $date->format('Y-m-d');
strtotime() is able to handle the format that you have and convert it into timestamp. Then you can use it to format date in whatever format you want with date() function:
$date_event = 'Fri, 30 Jun 2017 16:27:44 +0000 (UTC)';
echo date('d.m.Y H:i:s',strtotime($date_event));
//outputs: 30.06.2017 18:27:44
Here's working example:
https://3v4l.org/obfNB
use strtotime:
$date_event = 'Fri, 30 Jun 2017 16:27:44 +0000 (UTC)';
echo date('d.m.Y H:i:s',strtotime($date_event));

Is strtotime() having a bug?

Consider this code where we want to add or substract one second:
date_default_timezone_set("Europe/Amsterdam");
$time = 1477789199;
echo $time . ' - ' . date('r', $time) . "\n";
// 1477789199 - Sun, 30 Oct 2016 02:59:59 +0200
This is correct, as this timestamp is still just within DST (daylight savings time / summer time).
But now let's add one second to the timestamp integer, and exit DST:
$new = $time + 1;
echo $new . ' - ' . date('r', $new);
// 1477789200 - Sun, 30 Oct 2016 02:00:00 +0100
Hooray! PHP sees that one second later there is no DST anymore and shows a proper time string.
But what if we didn't add a second to the timestamp integer, but we used strtotime() to add that one second:
$new = strtotime('+1 second', $time);
echo $new . ' - ' . date('r', $new);
// 1477792800 - Sun, 30 Oct 2016 03:00:00 +0100
Yikes! We just went ahead by more than one hour instead of one second. And it doesn't even matter if you add one second, one hour, one day or one year you will always get one extra hour with it. Even if you add multiple years, you will only get one extra hour, which is weird because we enter and exit DST every year but you only get one extra hour regardless of how many years you add
But once we exit DST in October and subtract one second, all goes fine...
But then again. If we were in March and we have just entered DST, and we subtract one second, we observe exactly the same in reverse.
Wait, what ?! So ... ?
echo strtotime('+ 1 second - 1 second', 1477789199); // echoes 1477792799
Whoa ...
To me this sounds like a bug. Or is this 'by design'? Does anyone even know if this is documented somewhere, or whether it needs to be reported?
The behavior is "well documented" .... in a test:
See https://bugs.php.net/bug.php?id=30532 (which also presents your expected result as expected) and the related test file (which asserts that the current behavior is correct) https://github.com/php/php-src/blob/master/ext/date/tests/bug30532.phpt
<?php date_default_timezone_set("America/New_York");
echo date('Y-m-d H:i:s T', strtotime('2004-10-31 EDT +1 hour'))."\n";
echo date('Y-m-d H:i:s T', strtotime('2004-10-31 EDT +2 hours'))."\n";
echo date('Y-m-d H:i:s T', strtotime('2004-10-31 EDT +3 hours'))."\n";
/* 2004-10-31 01:00:00 EDT
2004-10-31 01:00:00 EST
2004-10-31 02:00:00 EST */
echo date('Y-m-d H:i:s T', strtotime('2004-10-31 +1 hour'))."\n";
echo date('Y-m-d H:i:s T', strtotime('2004-10-31 +2 hours'))."\n";
echo date('Y-m-d H:i:s T', strtotime('2004-10-31 +3 hours'))."\n";
/* 2004-10-31 01:00:00 EDT
2004-10-31 02:00:00 EST
2004-10-31 03:00:00 EST */
Note that in the former case the timezone (here: EDT) is being passed directly to the string, in the latter case it isn't.
In general strtotime is taking the timestamp (i.e. of 2004-10-31 - or in your specific case: the passed timestamp), converted to a representation with individual parameters, ignoring DST (i.e. individual hours, minutes, seconds, day, month, year etc.), the operation is applied to it and then converted back to the timestamp.
In particular:
echo date('r', strtotime('+ 0 second', 1477789199));
#> Sun, 30 Oct 2016 02:59:59 +0100
strtotime() throws the timezone after conversion away, i.e. only takes
Sun, 30 Oct 2016 02:59:59
and then applies the primary applicable timezone to your timezone location (i.e. Europe/Amsterdam), ending up with CET (primary!) - CEST is also possible, but only second choice.
Now, look back at the test above, just specify the originating timezone explicitly.
Thus, if you wish it to behave the way you need it:
echo date('r', strtotime('CEST', 1477789199));
#> Sun, 30 Oct 2016 02:59:59 +0200
echo date('r', strtotime('CEST + 1 second', 1477789199));
#> Sun, 30 Oct 2016 02:00:00 +0100
In fact, prepending 'CEST ' will be fine for all the times (as it will always fallback to CET if CEST is not matching and there's no overlap on CET -> CEST transition).

Convert a datetime format to match MySQL's datetime with PHP

I need a way to convert this format Tue, 20 Oct 2015 17:43:23 0000
to the datetime format that MySQL accepts.
I have tried to convert it with strtotime() but the function returns a negative number.
The problem appears to be the trailing zeroes, by trimming them from the date string it seems to work fine. The code below uses one of the Date constants - change that for the format desired.
$d='Tue, 20 Oct 2015 17:43:23 0000';
echo date( DATE_COOKIE, strtotime( trim($d,' 0000') ) );
outputs -> Tuesday, 20-Oct-15 17:43:23 BST
echo date( 'Y-m-d H:i:s', strtotime( trim($d,' 0000') ) );
outputs -> 2015-10-20 17:43:23
Try this with your time specified as the $inputtime variable:
strftime("%F %H:%M:%S", strtotime($inputtime))
The problem lies in your time format Tue, 20 Oct 2015 17:43:23 0000. Here 0000 means time zone. But it should have format + or - before value to work properly. Look at example:
echo strtotime("Tue, 20 Oct 2015 17:43:23 +0000");
Try this
$date = \DateTime::createFromFormat('r',your date);
$date = $date->format(format that you need);

Adding time to Nov. 2nd gives error

I'm having some problems adding a date.
$test = strtotime('nov 02 2014');
$test_date = date('D, M. jS, Y' ,(1.0*86400) + $test);
echo $test_date;
returns Sun, Nov. 2nd, 2014
changing input to nov 01 and nov 03 return the expected strings.
This should work for you:
$test = "nov 02 2014";
echo $test_date = date('D, M. jS, Y' ,strtotime($test . ' + 1 day'));
Daylight Saving Time ended on November 2, 2014. This means that November 2, 2014 lasted longer than the 86,400 seconds you're adding to the date!

Javascript time to PHP time

I have a time value in javascript for example 11:30:00.
Date {Mon Oct 22 2012 11:30:00 GMT+0800 (Taipei Standard Time)}
and I passed to a php file and converted it by:
$newTime = date('H:i:s', $theTime);
But, it return 05:36:00. What is the right way of concerting time?
Use myDate.getTime() instead, and then divide this by 1000 since PHP deals with seconds while JavaScript deals with milliseconds.
If you're looking to use PHP to parse the date/datetime, then you should use strtotime(). Something like:
$time = "Mon Oct 22 2012 11:30:00 GMT+0800";
echo date('Y-m-d h:i:s', strtotime($time));
Which would output:
2012-10-22 04:30:00
This output is GMT, which you can change if required.
EDIT:
If you're expecting 11:30:00, then try the following:
date_default_timezone_set('UTC');
$time = "Mon Oct 22 2012 11:30:00 GMT+0800";
echo date('Y-m-d h:i:s', strtotime($time));

Categories