Forum Moderators: coopster

Message Too Old, No Replies

Day and week number clarification please

I dont understand the three different results

         

Wayder

1:06 am on Dec 19, 2014 (gmt 0)

10+ Year Member Top Contributors Of The Month



date('w', strtotime("2014-12-14")) returns 0
date('W', strtotime("2014-12-14")) returns 50
date('w', strtotime("2014-12-15")) returns 1
date('W', strtotime("2014-12-15")) returns 51

If 2014-12-14 is day 0 of the week (the first day), then surely it should also be the same week as 2014-12-15

Also I use cron to mysqldump the database and pipe it to /path_to_file/db_backup-`date +\%Y`-week-`date +\%W`.sql.gz

The week number that is produced by this on 2014-12-14 is 49.

I dont understand why I get three different week numbers for what should be the same week. Can anyone clarify?

Thanks

penders

2:46 am on Dec 19, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If 2014-12-14 is day 0 of the week (the first day), then surely it should also be the same week as 2014-12-15


Well, you would think, but it depends when the week starts. Most traditional calendars see day 0 (the first day of the week) as Sunday - and this is what PHP uses for "w". However, for week numbers ("W"), PHP uses ISO-8601, in which weeks start on a Monday. So, that is the discrepancy in this instance.

If you want the ISO-8601 day of the week (1 to 7, Monday to Sunday) then use the "N" format character (added in PHP 5.1).

CRON... The week number that is produced by this on 2014-12-14 is 49


I think the week numbers here start from 0 (computer friendly), not 1 (as in PHP - human/calendar friendly).

Wayder

7:50 am on Dec 19, 2014 (gmt 0)

10+ Year Member Top Contributors Of The Month



That is an excellent explanation.

Thank you.