Page 1 of 2

Changing variables into date....

Posted: Tue Sep 24, 2002 4:23 pm
by DynamiteHost
Hi,

If I had this:

Code: Select all

<?
print $day . $month . $year;
?>
And when it prints I have this

Code: Select all

23 09 02

Would there be anyway to make it:

Code: Select all

23rd September 2002
So it gets the values from the variables and converts the date into a more readable format.

Please help, and I hope you can understand this :)

Thanks all :D

Posted: Tue Sep 24, 2002 4:28 pm
by Takuma
Use mktime() to create UNIX timestamp and then use date() to convert it to whatever you want.

Posted: Tue Sep 24, 2002 4:47 pm
by DynamiteHost
Thats no use to me :(

Those variables are pulled from a database which isn't a script I made and I don't really want to mess with it :cry:

Posted: Tue Sep 24, 2002 4:55 pm
by mydimension
do something like this:

Code: Select all

echo date("d B Y", mktime(0, 0, 0, $month, $day, $year));

Posted: Tue Sep 24, 2002 5:02 pm
by nielsene
Normally, I'ld do it using the built-in functions already referenced, but I guess you could do something like

Code: Select all

switch($day)
{
   case 1: case 21: case 31: $longDay=$day."st"; break;
   case 2: case 22:              $longDay=$day."nd";break;
   case 3: case 23:              $longDay=$day."rd":break;
   default:                           $longDay=$day."th";break;
}
$months = array("January","February","March","April","May","June",
                       "July","August","September","October","November","December");
$longMonth=$months&#1111;$month];

if ($year &lt; 37) // Y2K window method
    $longYear = "20$year";
else
    $longYear = "19$year";


echo "$longDay $longMonth $longYear";

Posted: Wed Sep 25, 2002 1:04 am
by Takuma
mydimension wrote:do something like this:

Code: Select all

echo date("d B Y", mktime(0, 0, 0, $month, $day, $year));
That's what I said but hey...

Posted: Wed Sep 25, 2002 5:27 am
by twigletmac
.
DynamiteHost wrote:Thats no use to me :(

Those variables are pulled from a database which isn't a script I made and I don't really want to mess with it :cry:
It's going to be kinda difficult (impossible) to do this without adding some code though... How were you hoping to achieve this if not by adding a couple more lines of code?

Mac

Posted: Wed Sep 25, 2002 12:05 pm
by DynamiteHost
What I meant by my last post is I don't want to go changing the date format which is put into the database.

I don't mind adding code which will apply when the variable is pulled from the database.

Sorry if my previous post wasn't clear enough.

Posted: Wed Sep 25, 2002 2:17 pm
by Takuma
You don't needed to, what's the format of date store in the db?

Posted: Wed Sep 25, 2002 2:30 pm
by DynamiteHost
The values are all 2 digits. Day, month and year are different fields.

for example, today would be:

$day = "25";

$month = "09";

$year = "02";

Posted: Wed Sep 25, 2002 3:24 pm
by Takuma
Exactly use mktime and date conbination like "mydimension" said...

Code: Select all

&lt;?php
echo date("d B Y", mktime(0, 0, 0, $month, $day, $year)); 
?&gt;

Posted: Thu Sep 26, 2002 4:49 am
by twigletmac
DynamiteHost wrote:What I meant by my last post is I don't want to go changing the date format which is put into the database.
I'm not sure why you think anybody is telling you (or giving you code) to do that. The code that's been posted deals with the variables once they been taken from the database and just formats them in the way that you wanted to.
DynamiteHost wrote:I don't mind adding code which will apply when the variable is pulled from the database.
Then try the code that's been posted. They really have given you the solution that you are looking for.

Mac

Posted: Thu Sep 26, 2002 11:44 am
by Coco
well since this has turned into a mktime() thread....

why does this:

Code: Select all

&lt;?php
echo date("d/M/y",mktime(date("H",time())+1,0,0,date("M",time()),date("d",time()),date("y",time())  ));
?&gt;
return 26/Dec/01
:?: :?

Posted: Thu Sep 26, 2002 12:11 pm
by nielsene
because mktime doesn't like strings and you're passing in a month name and not a month number... Why passing in a string causes that paticular date to be chosen I don't know, probably has to do with its integer representation.....
Try

Code: Select all

&lt;?php
$hour = date("H",time());
$day = date("d",time());
$month = date("m",time()); // month number not Name
$year = date("Y",time()); // four digit year
echo date("d/M/y",mktime($hour+1,0,0,$month,$day,$year));
?&gt;
Of course the easier way to do this is

Code: Select all

&lt;?php
echo date("d/M/y",time()+3600);
?&gt;

Posted: Thu Sep 26, 2002 12:20 pm
by Coco
thanks for that :)

i cant use time()+3600 because i am looking for the next hour dead on, not 1 hour in the future