Skip to content

Instantly share code, notes, and snippets.

@rvalenciano
Created December 9, 2014 23:58
Show Gist options
  • Save rvalenciano/c00f8b15e7c9c8e935a1 to your computer and use it in GitHub Desktop.
Save rvalenciano/c00f8b15e7c9c8e935a1 to your computer and use it in GitHub Desktop.
Problem of counting the number of days since the beginning of the year
<?php
function countDays($dateInString) {
// Write your code here
// To print results to the standard output you can use print
// Example:
// print "Hello world!";
$month = '';
$day = '';
$year = '';
list($month, $day, $year) = split('[/.-]', $dateInString);
if (checkdate($month, $day, $year)) {
// valid date ...
//print "Month: $month; Day: $day; Year: $year<br />\n";
$startTimeStamp = strtotime($year . "/01/01");
$endTimeStamp = strtotime($year . "/" . $month . "/" . $day);
$timeDiff = abs($endTimeStamp - $startTimeStamp);
$numberDays = $timeDiff/86400; // 86400 seconds in one day
// and you might want to convert to integer
$numberDays = intval($numberDays);
print $numberDays + 1;
}
else
{
print "Bad format";
}
}
// Do NOT call the countDays function in the code
// you write. The system will call it automatically.
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment