Relative Date calculation off - php

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

Related

I would like to get a date with strtotime. last fri or sat or sun on a month

I would like to get a date with strtotime.
A last Friday of a month and Saturday and Sunday if the month has sat and Sunday after last Friday.
How should I get it?
e.g. Aug 2021.
LastFriDay = 27 28 =sat 29 = sun.
I want to fetch “29. Aug”
e.g. July 2021.
LastFriday = 30 31 = sat. 1.Sep = sun
Then I want to fetch 31.Aug.
e.g Sep 2022
LastFriday = 30. 1.Oct = sat
Then The date should be 30.sep
$friOrSatOrSun = date("Y-m-d", strtotime("last fri of this month", strtotime($a_certain_day_of_month)));
Is there a good way to do that?

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!

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.

Formatting date and time to get date, month, year each in seperate var

In my json response of twitter API I get time stamp like this
Thu Mar 13 14:24:13 +0000 2014
I tried to format in this way:
$created_at = $thing->created_at;
$date = DateTime::createFromFormat('D M d H:m:s O Y', $created_at);
echo $created_at;
echo $date->format('H:m:s');
Which gives result like this:
Thu Mar 13 14:24:13 +0000 2014
2015:12:13 //formated result. How come 2015?????
Wed Mar 12 14:18:14 +0000 2014
2015:06:12
Tue Jan 21 12:50:17 +0000 2014
2018:02:21
Thu Dec 12 09:29:16 +0000 2013
2015:05:12
Why giving wrong result?
I want to get month, year in seperate variable.
You can simplify the creation of the DateTime by doing this:
$dt = new DateTime('#' . strtotime('Thu Mar 13 14:24:13 +0000 2014'));
This parses the date string to a Unix timestamp, and then creates a DateTime object.
echo $dt->format('Y-m-d H:i:s'); // yields the correct result.
You are using month format character m instead of minutes i, thats why you get "wrong" output.
$dt = new DateTime('Thu Mar 13 14:24:13 +0000 2014');
echo $dt->format('H:i:s');

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

Categories