[Challenge] The exact date (date representation challenge)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

[Challenge] The exact date (date representation challenge)

Post by Darhazer »

In MS Excel, date value is stored in number of days since 1.1.1900
Write a function that accept integer (numer of days) and returns the exact date. Note the leap years.

So, to check your program:

1 should return 1 Jan 1900 (so we start counting from 1)
14600 should return 22 Dec 1939
21961 should return 16 Feb 1960
40361 should return 3 Jul 2010 :)

edit: corrected the expected result for 21961
Last edited by Darhazer on Sat Jul 03, 2010 12:44 pm, edited 1 time in total.
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: [Challenge] The exact date (date representation challeng

Post by Apollo »

Code: Select all

<?php
if ($d=intval($_POST['d']))
{
  $dm = $d;
  for ($y=1900;;$y++)
  {
    $ly = !($y&3) && (($y%100) || !($y%400));
    $nd = $ly ? 366 : 365;
    if ($dm<=$nd) break;
    $dm -= $nd;
  }
  if ($ly) $dm += 365*2;
  $dm = date('j M',($dm*24-12)*60*60);
  print("Day number $d is $dm $y<hr>");
}
?>
<form method='post'>Enter day number: <input type='text' name='d'> <input type='submit'></form>
Oh and 21961 should return 16 Feb 1960 ;)
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: [Challenge] The exact date (date representation challeng

Post by AbraCadaver »

Code: Select all

function exact_date($days) {
	//$date = new DateTime('1899-12-31');  //use for your timezone
        $date = new DateTime('1899-12-31', new DateTimeZone('UTC'));  //use for specific timezone
	$date->modify("+$days days");
	return $date->format('j M Y');
}

foreach(array(0,1,14600,21961,40361) as $days) {
	echo exact_date($days)."\n";
}
[text]31 Dec 1899
1 Jan 1900
22 Dec 1939
16 Feb 1960
3 Jul 2010[/text]
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: [Challenge] The exact date (date representation challeng

Post by Apollo »

AbraCadaver wrote:DateTime
Oh, right.. THAT was the class for dealing with timestamps before 1970 ;)
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: [Challenge] The exact date (date representation challeng

Post by Darhazer »

Apollo wrote: Oh and 21961 should return 16 Feb 1960 ;)
Well, Excel thinks that 1900 is a leap year :lol:
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: [Challenge] The exact date (date representation challeng

Post by AbraCadaver »

Darhazer wrote:
Apollo wrote: Oh and 21961 should return 16 Feb 1960 ;)
Well, Excel thinks that 1900 is a leap year :lol:
http://support.microsoft.com/kb/214326
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: [Challenge] The exact date (date representation challeng

Post by Weirdan »

Darhazer wrote: Well, Excel thinks that 1900 is a leap year :lol:
Well, then your other dates were off. Interestingly, to correct for this bug google spreadsheets start counting from 1899-12-31
Post Reply