Page 1 of 1

Dates Problem ?

Posted: Sat Sep 07, 2002 8:08 am
by sjunghare
I need help writing a function for the following situation.

User inputs dates (as a single string) in the format of MM/DD/YYYY.

I need to convert this to YYYYXXX where XXX is the day of the year from 001 to 365.

As a seperate function, I need to convert YYYYXXX back to PHP native time format for conversion to MySQL format.

Any Ideas?!

Posted: Sat Sep 07, 2002 8:37 am
by Coco
you gotta tell it how many days per month...
what your doing is kinda similar to an add 1 hour type function
personally i would use a switch statement....
something along the lines of this:

Code: Select all

<?php
$month = substr($date, 0, 2);
$day = substr($date, 3, 2);//remebering to miss out the /
$year = substr($date, -4);

switch ($month)
{
   case 1:
         $temp = 31 + $day; //add 1 month to days into month
         $output = ($year * 1000)+$temp;
         echo "$output";
         break;
   case 2:
         $temp = 59 + $day /* add jan + feb (if gonna be used for a couple of years include leap year provision


And So On*/
?>
hope that helps... check the syntax tho cos im not so good (but i done something similar in C++ so i know how to do it 8) )

Posted: Sat Sep 07, 2002 12:06 pm
by twigletmac
Have a look at the calendar functions:
http://www.php.net/manual/en/ref.calendar.php
and the date and time functions:
http://www.php.net/manual/en/ref.datetime.php

Mac

Posted: Sat Sep 07, 2002 2:36 pm
by Takuma
How about change the date to UNIX timestamp and use functions like mktime() or strftime().
?>