4

strtotime() in PHP is quite powerfull function. One of it's features is relative dates.

For example this command:

echo date('Y-m-d', strtotime('Sunday this week'));

produces 2016-02-14 on my machine (today is "2016-02-12", Friday). Thus it supposes that first day of week is Monday. However in different locales countries first day of week is different. Is there a way to change this behaviour and make strtotime() think that first week day is Sunday?

Stalinko
  • 3,319
  • 28
  • 31
  • I didn't know that it was relative to locale, but if it's configured anywhere, it's most likely configured in the php.ini that your webserver uses. – Glubus Feb 12 '16 at 12:31
  • Have you fixed a timezone value for `date.timezone` in your `php.ini`? – Latheesan Feb 12 '16 at 12:32
  • Different versions may also lead to different interpretations (eg see changelog on http://php.net/manual/en/function.strtotime.php ). – Werner Feb 12 '16 at 12:35
  • @Glubus, I used wrong word. I also don't state that this is relative to locale. But I want to clear this up :) – Stalinko Feb 12 '16 at 12:51
  • @Latheesan, I tried everything what Dan suggested below. No one helps. – Stalinko Feb 12 '16 at 12:52
  • @Werner, I don't see anything about interpretation of first day of week in the docs. I googled much before asked this question ;) – Stalinko Feb 12 '16 at 12:53
  • You may also want to have a look at [this answer](http://stackoverflow.com/a/31768493/5049185). – syck Feb 12 '16 at 12:55
  • Looks like it's a bug. http://stackoverflow.com/questions/13798026/php-week-starts-on-monday-but-monday-this-week-on-a-sunday-gets-monday-next Solve it by doing "next monday -1 week" – Glubus Feb 12 '16 at 12:57
  • @Glubus, too dirty hack. I'm not sure it will work on different machines with different locales. I need a solution which will work the same on all machines. I'm ready to adjust some setting manually but IDK which one – Stalinko Feb 12 '16 at 13:01
  • @Stalinko I'm saying it's a "bug", not saying this is a cheat to just not figure out the correct solution. If you disagree with it being a bug, that's fine. I've been looking around and many posts claim this particular feature is bugged, so my suggestion is a workaround. – Glubus Feb 12 '16 at 13:04
  • @Glubus, if you are sure, please post a code returning first and last day of current week in English interpretation (that week begins with Sunday) which will work same on any machine with php 5.6 at least. If it will work I'll mark you answer as correct. – Stalinko Feb 12 '16 at 13:10
  • @Stalinko In your case, it looks that you would be better off using a custom function which would have to do a bit of calculation, but is thoroughly tested and sure to return the correct result, than to rely on any locale or other system or php settings. – syck Feb 12 '16 at 13:10
  • @syck, yeah I also feel I will end up with some custom simple function. Just had to ensure that php doesn't allow this funcationality out of the box. – Stalinko Feb 12 '16 at 13:11
  • @Stalinko On that page, it says "Prior to PHP 5.3.0, relative time formats supplied to the time argument of strtotime() such as this week, previous week, last week, and next week were interpreted to mean a 7 day period relative to the current date/time, rather than a week period of Monday through Sunday.". So the interpretation of 'this week' in 'Sunday this week' is also depending on PHP version... – Werner Feb 12 '16 at 13:50

2 Answers2

1

As discussed in the comments of the question, it may be better to rely on a custom function, which is tested and will most probably produce the same result on every machine.

A function like this could be:

<?php

function x() {
  return date('Y-m-d', date('N')==7 ? strtotime('today') : strtotime('last sunday'));
}

echo x();

You find a demo here.

If you have many machines to deploy your code to, you could additionally include a test script in the installation process which tests if it gets correct results from this (and other things that may vary depending on installation).

syck
  • 2,984
  • 1
  • 13
  • 23
  • Thank you for a simple solution. I've upvoted it but won't mark as correct answer because the entire question was about `strtotime` function. I still wait for an explanation with a link to the officials docs or bugtracker explaining how `strtotime` works with weeks – Stalinko Feb 12 '16 at 14:06
0

PHP 5.5 introduced the Internationalization extension, which among many useful functions provides and IntCalendar class. It has a static function getFirstDayOfWeek which can be used to get the first day of the week, based a locale.

Straight from the docs:

ini_set('date.timezone', 'UTC');

$cal1 = IntlCalendar::createInstance(NULL, 'es_ES');
var_dump($cal1->getFirstDayOfWeek()); // Monday
John C
  • 8,223
  • 2
  • 36
  • 47
  • It also allows to set the first day of the week, via [IntlCalendar::setFirstDayOfWeek](http://php.net/manual/en/intlcalendar.setfirstdayofweek.php). – syck Feb 12 '16 at 13:03
  • Thanks John. You solution will work most likely, but I'm not ready to install an additional PHP extension on all my servers just to calculate correct week day :) – Stalinko Feb 12 '16 at 13:04