Currently, I have the timezone set to America/New York.
I have a timestamp of 1448933400, thats Monday, November 30, 2015 - 08:30 PM. I would like to get a relative time from this current timestamp as the "First Sun of Next Month".
So I am doing the following code:
strtotime("First Sun of Next Month", 1448933400);
The problem I am having is, is the returned value of 1449378000.
This is not at 8:30 PM, but instead at 12:00 AM. Is there an easy way to get relative dates to retain the relative time of the source timestamp?
There are many possible answers, but what you can do is first calculate the time offset for the day of your timestamp and apply it to the result:
<?php
$stamp = 1448933400;
$offset = $stamp - strtotime(date("Y-m-d", $stamp));
var_dump(strtotime("First Sun of Next Month", $stamp) + $offset);
?>
Output
int(1449451800) // or "2015-12-06 20:30:00"
Related
I should be able to get the first Wednesday of each month, I can do it easily using:
$firstWedNextMonth = date ('d-m-Y', strtotime('first wednesday of next month'));
echo "The first wed of next month is: ".$firstWedNextMonth;
However, considering that the first Wednesday of next month is March 6th, the system must show this date until 3:00:00 on Thursday March 7th, at 3:00:01 instead it must indicate Wednesday, April 3rd. I do not know how to do it. Thank you
I think, this should work. +27 hours is 24 hours + your margin (3 hours). You can test it setting $now to whatever you want, instead of the current time.
$now = time();
$firstWedThisMonth = date ('d-m-Y', strtotime('first wednesday of this month', $now));
$firstWedNextMonth = date ('d-m-Y', strtotime('first wednesday of next month', $now));
$boundaryDateTime = strtotime('+27 hours', strtotime($firstWedThisMonth));
if ($now <= $boundaryDateTime) {
echo "The first wed of next month is: ".$firstWedThisMonth;
} else {
echo "The first wed of next month is: ".$firstWedNextMonth;
}
I just ran into a strange bug that has occurs in PHP running in Pacific Time (and probably others). I had code to get the first day of a week (sunday) given an arbitrary date (in UNIX timestamp) in that week:
$day = date('w', $date);
$start_of_week = date('Y-m-d', $date - ($day * 60*60*24));
echo $start_of_week;
Prints 2014-03-08
This works for every single date that I've tried, except for those in the week of March 9th, 2014, which happens to be the week of daylight savings time in the US. For those, $start_of_week is '2014-03-08', which is a Saturday.
When I run this code with the timezone set to GMT, I get the correct output ('2014-03-09').
Additionally, when I change the code to the following in PST, I get the correct output:
$day = date('w', $date);
$start_of_week = date('Y-m-d', strtotime("-$day day", $date));
echo $start_of_week;
Prints 2014-03-09
So...WTF? Why is there a difference between strtotime("-1 day", $date) and $date - 60*60*24? Seems like it's jumping between different timezones.
codepad example
When you use -1 day, it uses the time-of-day from $date, and just changes the date. When you use - 60 * 60 * 24 it rolls the clock back 24 hours. On the day that DST changes, there are only 23 hours in the day, so it goes an hour too far.
– Barmar Mar 19 '14 at 22:11
Ok, So im trying to figure out the best way in php to write (or print) the 1st, 2nd, 3rd, 4th, & 5th date of the current week (starting with Monday) in php.
For example... Using the week of the 4th through the 8th of February...
I would need a script for the 1st day of the week (starting with Monday) displayed as February 04, 2013 and then I would need the same script four more times to display Tues, Wed, Thurs, & Fri...
All-together I would end up with 5 scripts or one script that I could copy and manipulate to work the way I need it to...
Also, If you could tell me how to do the same for Saturday and Sunday that would be greatly appreciated as-well.
If there is anything that you do not understand, please let me know and I will try my hardest to clarify...
Thanks in advance!
First you need to get the "current" week's Monday. To do this, I would suggest calling date("N") and see if it's 1. If it is, then now is the Monday you want. Otherwise last monday is. Pass that to strtotime to get the timestamp corresponding to the first monday of the week. Then repeatedly add 24 hours (24*3600 seconds) to get each day.
$startofweek = date("N") == 1 ? time() : strtotime("last monday");
for($i=0; $i<5; $i++) {
$day = $startofweek + 24*3600*$i;
echo "Day ".($i+1).": ".date("d/M/Y",$day)."<br />";
}
You can use strtotime with special parameters. like:
$time = strtotime('monday this week');
$time = strtotime('today');
$time = strtotime('next monday');
$time = strtotime('previous monday');
Same goes for every other days of the week
I'm looking for a reliable way to return the full date of a specified weekday (e.g. "Mon") for the current week.
Since today is Wednesday, June 13, 2012, I expected <?php echo date("Y-m-d", strtotime('Mon this week')); ?> to result in 2012-06-11, but instead php returns 2012-06-18 as though it interprets this week as meaning next week. Why this behavior and what should I be doing?
Thanks.
--Jeff
date( 'Y-m-d', strtotime( 'last Monday', strtotime( 'Sunday' ) ) );
This searches for the Monday previous to the next Sunday.
According to the documentation php relative date formats.
Then Monday this week would first advance to the next Monday and then process the relative text of this week.
dayname: Moves to the next day of this name unless it is the current day then it will not advance. In other words if the current date was June 11, then strtotime('Monday this week') would return June 11 whereas if the current date was June 13 then strtotime('Monday this week') would return June 19.
i think this is the solution for your problem:
$monday_date = date("Y-m-d", mktime(0,0,0, date("m"), date("j")-(date("w")+1), date("Y")));
I'm trying to get the unix time for date strings that are formatted like so:
'second sunday of march 2010'
'first sunday of november 2010'
I was under the impression that strtotime could handle such a string, but apparently not, as this returns false. How can I convert to unix time when given a day of week, which one of those in the month (ie. first, second, etc.), a month and a year.
This should be possible with strtotime. You could try generating a timestamp of the first day of march using mktime() and adding that as a 2nd parameter (leaving just "first sunday" in the string part):
$timestamp = mktime (0,0,0,3,1,2010); // 1st of march
$first_sunday = strtotime("first sunday", $timestamp);
Not sure how this will handle the first day (March 1st) actually being a sunday. Make sure you check that out.
Also, and maybe this more reliable, check this out - the poster says he got good results with the following notation (quoting):
<?php
strtotime('+0 week sun nov 2009'); // first sunday in nov 2009
strtotime('+1 week sun nov 2009'); // second sunday
strtotime('-1 week sun nov 2009'); // last sunday in oct 2009
?>
As always with strtotime, whatever you pick, make sure you test well, especially the edge cases (1st day of month is a sunday, last day of last month was a sunday....)
Your code works for me on PHP 5.3.0. What version of PHP are you using?
<?php
date_default_timezone_set("Europe/Oslo");
$time_march = strtotime('second sunday of march 2010');
$time_november = strtotime('first sunday of november 2010');
echo date("Y-m-d", $time_march) . " (timestamp: $time_march)\n";
echo date("Y-m-d", $time_november) . " (timestamp: $time_november)\n";
?>
gives:
2010-03-14 (timestamp: 1268521200)
2010-11-07 (timestamp: 1289084400)