Day of the Year – closingtags </>
Categories
PHP

Day of the Year

The most recent challenge I’ve completed was to input a date in plain english (eg. November 19th) and output what day of the year that was (day #323). This was a pretty fun challenge, and was simple enough.

https://www.reddit.com/r/dailyprogrammer/comments/pzo4w/2212012_challenge_13_easy/

<?php
	function dayofyear($date, $leap) {
		$datenum = explode(" ", date('m d', strtotime($date)));
		$months = array(31,28,31,30,31,30,31,31,30,31,30,31);
		$day = 0;
		if($leap) {
			$months[1] = 29;
		}
		for ($i=0; $i < intval($datenum[0]) - 1; $i++) {
				$day = $day + $months[$i];
		}
		return $day + $datenum[1];
	}
	echo dayofyear('November 19th', false);
?>

 

By Dylan Hildenbrand

Author and full stack web developer experienced with #PHP, #SvelteKit, #JS, #NodeJS, #Linux, #WordPress, and #Ansible. Check out my book at sveltekitbook.dev!

Do you like these posts? Consider sponsoring me on GitHub!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.