Web Hosting Talk







View Full Version : Date convirsion with php, is it possible ?


roar1
08-23-2002, 03:40 PM
Hi, I've just started php and am trying to convert a day number for example day 254 of year 2002 to format day of month of year. I have worked out how to make a program that soes this but it would be quite long and a pain to do, is there any function in PHP that would allow you to do this easily?:o

michaeln
08-23-2002, 04:05 PM
If this isn't what you are looking for let me know...

The following code outputs 09-11-02

<?
echo date ("m-d-y", mktime (0,0,0,1,254,2002));
?>


If you would rather it output it in 09/11/02 format change the m-d-y to m/d/y

Other than that I am pretty sure I understood you.

Above the 1 is the month of January.
The 245 is the days from day 0 of that month. The 2002 is the day.

So basically mktime() returns the UNIX time stamp for 245 days after Jan 0, 2002. Then the date function converts it to m/d/y format...

EDIT:
If you would rather call a function instead of useing that format in each location you can do this.

function return_date($day, $year)
{
return date("m/d/y", mktime (0,0,0,1,$day,$year));
}


Then you can call it like:

$my_date = return_date('254', '2002');
echo $my_date;

Regards,
Michael

nathanp
08-23-2002, 04:27 PM
sounds pretty easy, you need a custom script for this.

roar1
08-25-2002, 03:21 AM
Thanks, exactly what I was looking for :)

combs
08-25-2002, 10:28 AM
Good you made a function. Apply it in a global file so that it can be called from any module.

roar1
08-25-2002, 04:05 PM
Sorry what global file?

michaeln
08-25-2002, 07:10 PM
I think he simply means put it in a file named whatever you wish, lets say global.php.

Then at the top of each of the scripts in your site you put:
require("global.php");
if the file is in the same directory as the global file or
require("/path/to/global/file/global.php");
if the file is not in the same directory.

After that any variables or functions in global.php can now be used in the file you put the require statement in....

roar1
08-26-2002, 03:34 AM
Oh thanks :) Now I understand, actually I only needed it once in the script so this wont be necessary