Adding time to Nov. 2nd gives error - php

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!

Related

Converting date from JSON response in 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);

Relative Date calculation off

I am trying to calculate dates relative to a certain date, but Im getting some very unusual responses. Can someone explain what I am doing wrong? I am US EST if it matters.
<?php
$firstweek_firsttime = date('D M j', strtotime("June 2016 first Sunday"));//June 19th 2016
$firstweek_lasttime = date('D M j', strtotime("June 2016 second Saturday"));
$ret=array(
"Session #1. The week of ".$firstweek_firsttime." to ".$firstweek_lasttime." - ",
"Session #2. The week of ".date('D M j', strtotime("$firstweek_firsttime next Sunday"))." to ".date('D M j', strtotime("$firstweek_lasttime next Saturday"))." - ",
"Session #3. The week of ".date('D M j', strtotime("$firstweek_firsttime +10 day"))." to ".date('D M j', strtotime("$firstweek_lasttime +10 day"))." - "
);
?>
<ul>
<?php
foreach($ret as $wk)
{
?>
<li><?php echo($wk);?></li>
<?php
}
?>
What I am getting:
The week of Sun Jun 19 to Sat Jun 18 -
The week of Thu Jan 1 to Thu Jan 1 -
The week of Wed Jul 1 to Tue Jun 30 -
Goal:
The week of Sun Jun 19 to Sat Jun 25 -
The week of Sun Jun 26 to Sat Jul 2 -
The week of Sun Jul 3 to Sat Jul 9 -
This works for me.
Where "you" are has no bearing on your dates unless you set your timezone.
The date/time is set based on the server location.
It's a bit cumbersome, if I can find a better method I'll update my answer.
UPDATE
strtotime("$firstweek_firsttime"); is the equivalent of writing strtotime("Sun Jun 5");will output 1433635200 (which, as of today, is actually Sun Jun 7 2015 00:00:00) because no year is indicated, the server defaults to the current year
strtotime("next sunday"); will output 1441497600 (which, as of today, is equal to Sun Sep 6 2015 00:00:00
but
strtotime("$firstweek_firsttime next sunday"); is invalid markup and will output nothing
so, since the timestamp is empty the date is automatically set to Jan 1, 1970
The same goes for strtotime("$firstweek_lasttime next Saturday")
strtotime("$firstweek_firsttime +10 days") is the same as strtotime("Sun Jun 5 +10 days")
without a Year the server defaults to the current year and writes it as strtotime("Sun Jun 7 2015 +10 days") because June 7 is the first Sunday for June in 2015
The same goes for strtotime("$firstweek_lasttime +10 day")
All that being said... the simple solution to your question is adding the year to your date format for $firstweek_firsttime and $firstweek_lasttime. This will keep your date in the year that you expect like so...
<?php
$firstweek_firsttime = date('D M j Y', strtotime("June 2016 first Sunday")); // Sun Jun 5 2016
$firstweek_lasttime = date('D M j Y', strtotime("June 2016 second Saturday"));
If you don't want to output the year to the browser simply change your first array item to...
"Session #1. The week of ".date('D M j', strtotime("$firstweek_firsttime"))." to ".date('D M j', strtotime("$firstweek_lasttime"))." - ",
Reference
modify date

Parsing dates with inconsistent formats in PHP

I have two queries, both related to dates.
1) I have dates in these formats, which I'm looking to normalise into the same format before saving into a database:
Saturday 26 July
Monday 28 - Wednesday 30 July
July 24th, 2014
Thu 4 Sep
Thu 28 Aug — Fri 19 Sep
24-07-2014
Single days are quite easy to work out using strtotime(), but ranges of dates are a bit more tricky.
This, for example, doesn't work:
$dateString = "Monday 28 - Wednesday 30 July";
if (strpos($dateString, "-")) {
$datePieces = explode("-", $dateString);
$startDate = strtotime($datePieces[0]);
$endDate = strtotime($datePieces[1]);
} else {
$startDate = strtotime($dateString);
$endDate = strtotime($dateString);
}
echo '<pre>';
echo date('d F Y', $startDate);
echo '<br/>';
echo date('d F Y', $endDate);
Because the month is only on one side of the explode(), doing it this way returns:
01 January 1970
30 July 2014
2) I need a way of working out what year the date is (it will always be in the future). Something along the lines of:
if (the month in the date string has elapsed) {
the year of the date is this year + 1
}
As long as each source provides you with a consistent format you can use DateTime() and DateTime::createFromFormat() to process the dates for you.
//Saturday 26 July
$date = DateTime::createFromFormat('l j F', 'Saturday 26 July');
//July 24th, 2014
$date = new DateTime('July 24th, 2014');
//Thu 4 Sep
$date = DateTime::createFromFormat('D j M', 'Thu 4 Sep');
//Thu 28 Aug — Fri 19 Sep
list($start, $end) = explode(' - ', 'Thu 28 Aug — Fri 19 Sep');
$start = DateTime::createFromFormat('D j M', $start);
$end = DateTime::createFromFormat('D j M', $end);
//24-07-2014
$date = new DateTime('24-07-2014');
I'm going to leave handling Monday 28 - Wednesday 30 July to you since you'll need to do a little more work to get the month from the second date and apply it to the first. But this should show you how to go about this.

timestamp delete hours and seconds

i have a timestamp 1390107800 is Sun, 19 Jan 2014 05:03:20 GMT How could get this date? Sun, 19 Jan 2014 00:00:00 GMT
I want any given timestamp add the first hour of the day it would be 00:00
thanks.
You should be able to hard-code the zeros without issues but escape the GMT with back-slashes to make them literally show:
echo date('D, d M Y 00:00:00 \G\M\T', 1390107800);
// another option
echo date('D, d M Y', 1390107800).' 00:00:00 GMT';
For future reference: date
Just hardcode the hours minutes and second..?
date("d/m/Y 00:00:00", $timestamp);

Date formatting in PHP

I've a date formatted like "Tue Jan 05 11:08:27 +0000 2010" and I want to convert it's format to "yyyy-mm-dd 00:00" in PHP.
How can I do that?
convert it to a PHP date object with strtotime() then output it with date()
EDIT
Some more detail; try:
$time = strtotime('Tue Jan 05 11:08:27 +0000 2010');
echo date("Y-m-d h:i", $time);
Y = 4 digit year
m = 2 digit month (with leading 0)
d = 2 digit month (with leading 0)
h = 12 hour time (leading 0)
i = minutes (with leading 0)
http://php.net/manual/en/function.date.php
for all the formatting options
$time_string = 'Tue Jan 05 11:08:27 +0000 2010';
$formated_time = date('Y-m-d h:i', strtotime($time_string));
echo $formated_time;
strtotime + date
Agree with Erik, if you want to do it in one line.
Solution
$date = date('Y-m-d H:i:s', strtotime('Tue Jan 05 11:08:27 +0000 2010'));

Categories