Changing variables into date....

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

DynamiteHost
Forum Commoner
Posts: 69
Joined: Sat Aug 10, 2002 5:33 pm

Changing variables into date....

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

Post by Takuma »

Use mktime() to create UNIX timestamp and then use date() to convert it to whatever you want.
DynamiteHost
Forum Commoner
Posts: 69
Joined: Sat Aug 10, 2002 5:33 pm

Post 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:
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post by mydimension »

do something like this:

Code: Select all

echo date("d B Y", mktime(0, 0, 0, $month, $day, $year));
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

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

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

Post 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
DynamiteHost
Forum Commoner
Posts: 69
Joined: Sat Aug 10, 2002 5:33 pm

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

Post by Takuma »

You don't needed to, what's the format of date store in the db?
DynamiteHost
Forum Commoner
Posts: 69
Joined: Sat Aug 10, 2002 5:33 pm

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

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

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

Post 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
:?: :?
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

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

Post 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
Post Reply