Dates Problem ?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
sjunghare
Forum Newbie
Posts: 16
Joined: Tue Sep 03, 2002 6:22 am
Location: Pune

Dates Problem ?

Post 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?!
Coco
Forum Contributor
Posts: 339
Joined: Sat Sep 07, 2002 5:28 am
Location: Leeds, UK
Contact:

Post 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) )
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

How about change the date to UNIX timestamp and use functions like mktime() or strftime().
?>
Post Reply