Project Euler #19 code seems right. What am I missing? - php

Problem 19:
You are given the following information, but you may prefer to do some
research for yourself.
1 Jan 1900 was a Monday.
Thirty days has September, April, June and
November.
All the rest have thirty-one, Saving February alone, Which
has twenty-eight, rain or shine. And on leap years, twenty-nine.
A leap year occurs on any year evenly divisible by 4, but not on a
century unless it is divisible by 400.
How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?
I thought that using PHP for this would be a breeze since it has so many built-in time and date functions. My code is really quite simple, so I'm having a hard time seeing what I'm doing that's wrong.
My code:
<?php
echo "<pre>";
$sunday_count = 0;
for( $year = 1901; $year <= 2000; $year++ ) {
for( $month = 1; $month <= 12; $month++ ) {
// Produces a date in format: 1/1/2000
$date = $month . "/1/" . $year;
$time = strtotime( $date );
$is_sunday = ( date( 'l', $time ) == "Sunday" );
echo "$date "
. ( $is_sunday ? 'was a Sunday. ' : '' )
. "<br>";
if( $is_sunday ) $sunday_count++;
}
}
echo "Answer: $sunday_count";
echo "</pre>";
?>
The solution my code comes up with is 169, which is not correct. Any idea?
Edit 1
The solution is supposed to be 171.
Using Wolfram Alpha and my Windows clock, I've doubled checked several of the Sundays which my code reports. All of them check out OK.
So it seems my code is reporting valid and legitimate Sundays, but somehow it has missed two of them.
Edit 2
I made the following minor change to the formatting of the date in my code:
$date = sprintf('%d-%02d-01', $year, $month); // formats yyyy-mm-dd
I then used #MadaraUchiha's code to generate an array containing the 171 correct dates.
After comparing his dates with mine, these are the two missed dates:
1901-09-01
1901-12-01
Edit 3
Codepad also shows that these dates are not Sundays (but they really should be).
And I am certain that the dates are correctly being interpreted as YYYY-MM-DD because one of the dates my code offers to the solution is 2000-10-01, which would only be a Sunday if 10 is the month, not the day.
Edit 4
So apparently if on a 32 bit system, Unix timestamps won't work outside of the range:
Fri, 13 Dec 1901 20:45:54 GMT
to
Tue, 19 Jan 2038 03:14:07 GMT

The reason it might not work on some systems using timestamps is that the range of a Unix timestamp on 32-bit systems is from Fri, 13 Dec 1901 20:45:54 GMT to Tue, 19 Jan 2038 03:14:07 GMT, so you miss almost all months in the first year.
64-bit systems have larger integers which makes the range bigger (in PHP).

I get 171 with this (much shorter, and more readable) code, using DateTime objects:
header("Content-type: text/plain"); //Browser display
$time = new DateTime("1901-01-01");
$end = new DateTime("2000-12-31");
$counter = 0;
while (!$time->diff($end)->invert) { //$time isn't greater than $end
$time->modify("+1 month");
if ($time->format("l") == "Sunday") {
$counter++;
echo $time->format("Y-m-d") . " was a Sunday!\n";
}
}
echo "\nTotal number of Sundays: $counter";
When using DateTime objects, you're treating dates as Dates and not as numbers or strings. This gives you a huge flexibility advantage over any other approaches.

The problem on this code is the line
$date = $month . "/1/" . $year;
this should be
$date = "1/".$month."/" . $year;
You mixed the places of months and days.

Related

Add 30 days to date [duplicate]

This question already has answers here:
Add number of days to a date
(20 answers)
Closed 6 years ago.
Hello all i am trying to add 30 days to my date. I am using below coding.
<?php
$next_due_date = date('05/06/2016', strtotime("+30 days"));
echo $next_due_date;
?>
But it is returning to "05/06/2016" only!
Please help me!
Do not use php's date() function, it's not as accurate as the below solution and furthermore it is unreliable in the future.
Use the DateTime class
<?php
$date = new DateTime('2016-06-06'); // Y-m-d
$date->add(new DateInterval('P30D'));
echo $date->format('Y-m-d') . "\n";
?>
The reason you should avoid anything to do with UNIX timestamps (time(), date(), strtotime() etc) is that they will inevitably break in the year 2038 due to integer limitations.
The maximum value of an integer is 2147483647 which converts to Tuesday, 19 January 2038 03:14:07 so come this time; this minute; this second; everything breaks
Source
Another example of why I stick to using DateTime is that it's actually able to calculate months correctly regardless of what the current date is:
$now = strtotime('31 December 2019');
for ($i = 1; $i <= 6; $i++) {
echo date('d M y', strtotime('-' . $i .' month', $now)) . PHP_EOL;
}
You'd get the following sequence of dates:
31 December
31 November
31 October
31 September
31 August
31 July
31 June
PHP conveniently recognises that three of these dates are illegal and converts them into its best guess, leaving you with:
01 Dec 19
31 Oct 19
01 Oct 19
31 Aug 19
31 Jul 19
01 Jul 19
Please try this.
echo date('m/d/Y',strtotime('+30 days',strtotime('05/06/2016'))) . PHP_EOL;
This will return 06/06/2016. Am assuming your initial date was in m/d/Y format. If not, fret not and use this.
echo date('d/m/Y',strtotime('+30 days',strtotime(str_replace('/', '-', '05/06/2016')))) . PHP_EOL;
This will give you the date in d/m/Y format while also assuming your initial date was in d/m/Y format. Returns 05/07/2016
If the input date is going to be in mysql, you can perform this function on mysql directly, like this.
DATE_ADD(due_date, INTERVAL 1 MONTH);
The first parameter is the format, not the current date.
<?php
$next_due_date = date('d/m/Y', strtotime("+30 days"));
echo $next_due_date;
Demo: https://eval.in/583697
If you want to add the 30 days to a particular starting date use the second parameter of the strtotime function to tell it where to start.
When in doubt about how a function works refer to the manual.
http://php.net/manual/en/function.strtotime.phphttp://php.net/manual/en/function.date.php
You need to provide date format like d/m/Y instead of 05/06/2016
Try
$old_date = '05-06-2016';
$next_due_date = date('d-m-Y', strtotime($old_date. ' +30 days'));
echo $next_due_date;
$date = "1998-08-14";
$newdate = strtotime ( '30 day' , strtotime ( $date ) ) ;
$newdate = date ( 'Y-m-j' , $newdate );
echo $newdate;
Found the above code

Building a weekly dropdown with php

I have reports i run, they are based on weekly reports from Wed - Tue each week example here:
else if($_GET['week'] =='aug13th2014'){
$date_from = "2014-08-13 00:00:00";
$date_to = "2014-08-19 23:59:59";
$date_display = "Aug 13th to Aug 19th 2014";
}
Then i echo the select
echo "<option value='aug13th2014'>Aug 13th to Aug 19th 2014</option>";
So if a user selects that week and submits the form, then it uses the $date_from and $date_to to run my reports and return the values for that week.
This all works great but its a pain in the butt to have to manually create those else if statements every single week and if i am not concentrating then i can make mistakes on the date values.
What i want to do is automate it. So im thinking ill use a script that does the following:
Note that even though i say from wednesday to tuesday its officially
Get Todays Date
Find the date for the previous Wednesday
Check if 168 hours (7 days) has elapsed since Wed at 00:00:00
If not then find the previous Wednesday and then get the date for 168 hours after that point.
This will give me 2 dates now. Wed 13th Aug 2014 and ill set the time to 00:00:00 and then ill also have the date for Tuesday 19th Aug.
Then ill decide how many weeks i want to show in the dropdown and loop that many times
Then i will need to deduct 168 hours from both wednesday and tuesdays date in the next loop to get the previous week.
Repeat that for however many loops i get
My question is before i got to all that hassle is there a more efficient way to do this? Maybe a php class available for this very type of problem? I am not using any frameworks and its not worth converting just for this job. Also are there any obvious flaws in my approach or it looks good?
One of my concerns is handling Daylight Savings Time where the time goes back and forward one hour, i guess i could manually change it twice a year but maybe there is a simpler solution
The PHP function strtotime can help you here.
echo strtotime('Last Wednesday');
I wouldn't do:
echo "<option value='aug13th2014'>Aug 13th to Aug 19th 2014</option>";
Rather use a timestamp so code your dropdown like so:
$date = strtotime('Last Wednesday');
$weeks = 5;
$i = 1;
echo '<select>';
while ($i <= $weeks) {
echo '<option value="'.$date.'">Week of ' . date('D d M', $date) . '</option>';
$date = strtotime( date('Y-m-d', $date) . ' + 7 Days');
$i++;
}
echo '</select>';

PHP seems to be returning incorrect weekdays

First of all, here is my code:
<?php
$year = 1006;
while ($year <= 1996)
{
$date = '1/26/' . $year;
if (date('l', strtotime($date)) == 'Monday')
{
echo $date . PHP_EOL;
}
$year += 10;
}
?>
This is supposed to output the January 26s between 1000 and 1999 where the year ends with a 6 and the date falls on a Monday. Here is the output:
1/26/1006
1/26/1096
1/26/1136
1/26/1186
1/26/1226
1/26/1316
1/26/1366
1/26/1406
1/26/1496
1/26/1536
1/26/1586
1/26/1626
1/26/1756
1/26/1846
1/26/1976
All seems well. However, when I look at few of these years on timeanddate.com, the weekdays do not seem to have anything in common:
1/26/1006 falls on a Saturday
1/26/1406 falls on a Tuesday
1/26/1756 does fall on a Monday
What's going on here?
strtotime() converts a string representation of a date into a unix timestamp - the number of seconds since jan 1, 1970. You may start to see the problem here...
A timestamp is typically stored in a 32 bit integer, which gives a range between 1970 and 2038. Trying to generate timestamps for dates outside that range will give you undefined behavior.
-- edit --
From the php manual:
The valid range of a timestamp is typically from Fri, 13 Dec 1901
20:45:54 GMT to Tue, 19 Jan 2038 03:14:07 GMT. (These are the dates
that correspond to the minimum and maximum values for a 32-bit signed
integer). However, before PHP 5.1.0 this range was limited from
01-01-1970 to 19-01-2038 on some systems (e.g. Windows).

How to find a start date & end date of any given year & month

I have problem in php find start date & end date of month & year , when i know the year and month ?
ex:
input - > year = 2011 , month = 08
output -> start date = 01 , end date = 31
echo date('m-01-Y 00:00:00',strtotime('this month')) . '<br/>';
echo date('m-t-Y 12:59:59',strtotime('this month')) . '<br/>';
Start date will always be 1 and you can find the end date with the following function.
cal_days_in_month(CAL_GREGORIAN, $month, $year);
reference:
cal_days_in_month ( int $calendar , int $month , int $year ) : int
Use date (t format gives days in year) and create a time for it:
$year = 2011; $month = 6;
$starts = 1;
$ends = date('t', strtotime($month.'/'.$year)); //Returns days in month 6/2011
i really can't understand you clearly but to get the start date here is the code
date('Y-m-d');
this code above will get you the day of today
and to get the end of the running month this code i used before
date(’Y-m-d’,strtotime(’-1 second’,strtotime(’+1 month’,strtotime(date(’m').’/01/’.date(’Y').’ 00:00:00′))));
i hope this help you in your issue
You should look into strtotime:
echo date("D, M j, Y", strtotime("FIRST DAY OF MAY 2012"));
// Tue, May 1, 2012
echo date("D, M j, Y", strtotime("last DAY june 2012")); // gotcha! using June.
// Thu, May 31, 2012
PHP may have a more elegant way of doing this, but if you want a generic algorithm, here's what you need to do...
All months other than February have a fixed number of days. February has 29 only when it's a leap year. Here are the rules to check if it's a leap year:
If the year is evenly divisible by 4, go to step 2. Otherwise, go to step 5.
If the year is evenly divisible by 100, go to step 3. Otherwise, go to step 4.
If the year is evenly divisible by 400, go to step 4. Otherwise, go to step 5.
The year is a leap year (February has 29 days).
The year is not a leap year (February has 28 days).
hi Try this way u can do it
function firstOfMonth() {
return date("m/d/Y", strtotime(date('m').'/01/'.date('Y').' 00:00:00'));
}
function lastOfMonth() {
return date("m/d/Y", strtotime('-1 second',strtotime('+1 month',strtotime(date('m').'/01/'.date('Y').' 00:00:00'))));
}
$date_start = firstOfMonth();
$date_end = lastOfMonth();`
$year = '2017';
$month = '05';
echo date("$year-$month-01");
echo "<br>";
echo date("$year-$month-t");
shortest solution in my own opinion.

php program to find the my birthday where day name "Monday" for another 100 years

I was born in 1986-04-21, which is Monday. My next birthday with day name "Monday" is 1997-04-21 and so on.
I wrote the program to find upto 100 year to find which year my birthday comes with matching day name that is monday.
This is the code:
<?php
date_default_timezone_set('Asia/Calcutta');
for($year = 1986; $year < 2086; $year++) {
$timestamp = mktime(0, 0, 0, 4, 21, $year);
if(date('l', $timestamp) == 'Monday') {
echo date('Y-m-d, l', $timestamp) . "\n";
}
}
?>
This is the output of the program:
1986-04-21, Monday
1997-04-21, Monday
2003-04-21, Monday
2008-04-21, Monday
2014-04-21, Monday
2025-04-21, Monday
2031-04-21, Monday
2036-04-21, Monday
Now my problem is why PHP is not supporting before 1970 and after 2040.
So how can I get the birthday after 2040 or before 1970?
There's no need to use any special date processing classes or functions at all.
Your birthday is after the leap day in February, so from one year to the next it'll either be one day (365 % 7) or (on leap years) two days (366 % 7) later in the week than it was the year before.
$year = 1985; // start year
$dow = 0; // 0 for 1985-04-21 (Sunday)
while ($year < 2100) {
$year++; $dow++;
if ($year % 4 == 0 && ($year % 100 != 0 || $year % 400 == 0)) {
$dow++; // leap year
}
$dow %= 7; // normalise back to Sunday -> Saturday
if ($dow == 1) {
printf("%04d-%02d-%02d is a Monday\n", $year, 4, 21);
}
}
This code will work on any version of PHP.
If you're on PHP 5.3, you can use the DateTime class and add DateIntervals. It is based on 64-bit integers and doesn't have the year 2038 problem.
Basic example:
<?php
$year = DateInterval::createFromDateString('1 year');
$date = new DateTime('1986-04-21');
$date->add($year);
echo $date->format('Y-m-d') . "\n"; // Repeat 100 times
documentation on createFromDateString() is here.
For the reason why you can't go before 1970 or past 2038 see the date manual:
The valid range of a timestamp is
typically from Fri, 13 Dec 1901
20:45:54 GMT to Tue, 19 Jan 2038
03:14:07 GMT. (These are the dates
that correspond to the minimum and
maximum values for a 32-bit signed
integer). However, before PHP 5.1.0
this range was limited from 01-01-1970
to 19-01-2038 on some systems (e.g.
Windows).

Categories