PHP and Dates

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

Post Reply
tdelobe
Forum Commoner
Posts: 41
Joined: Thu Aug 07, 2003 2:28 pm
Location: washington, dc

PHP and Dates

Post by tdelobe »

Ok, so I have this function that I am using to stuff the year month and day into an array so that I can rearrange the order of them on a webpage. However, I was wondering if anyone could help me add functionality to this so that I could show the weekday, and the month name (i.e. January) too. I can not seem to get it to work. Let me know if you have a solution, thanks.

Code: Select all

function getDateArray( $dstamp ) {
        $ddate=substr( $dstamp, 0, 10 );
        list ($year, $month, $day ) = split ('ї/.-]', $ddate);
        $da = array();
        $daї"month"]=$month;
        $daї"day"]=$day;
        $daї"year"]=$year;
        return $da;
    }
here is the code I am using to show the date on the webpage...

Code: Select all

<? $releasedate = getDateArray( $thispage&#1111;"releasedate"] ); ?>
<?=$releasedate&#1111;"month"]?>-<?=$releasedate&#1111;"day"]?>-<?=$releasedate&#1111;"year"]?>
Any help would be greatly appreciated. Thanks.
tdelobe
Forum Commoner
Posts: 41
Joined: Thu Aug 07, 2003 2:28 pm
Location: washington, dc

quick clarification

Post by tdelobe »

The date should look like this...

Wednesday, January 1, 2003
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Where does $dstamp come from? If it's a database, which one as it would probably be simpler to format the date in the SQL statement.

Mac
tdelobe
Forum Commoner
Posts: 41
Joined: Thu Aug 07, 2003 2:28 pm
Location: washington, dc

Post by tdelobe »

i am using a SQLServer DB.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Since my knowledge of SQL is zilch, here's a bit of PHP pseudo-code for using [php_man]mktime[/php_man]() and [php_man]date[/php_man]() to get the result you want:

Code: Select all

<?php

	$day = 1;
	$month = 1;
	$year = 2003;

	$datestamp = mktime(0, 0, 0, $month, $day, $year);
	$formatted_date = date('l, F j, Y', $datestamp);
	
	echo $formatted_date;
?>
If you can find a function in SQLServer that allows you to format the date then that would cut out a few lines of code.

Mac
tdelobe
Forum Commoner
Posts: 41
Joined: Thu Aug 07, 2003 2:28 pm
Location: washington, dc

Post by tdelobe »

thanks for the code, however, I cannot seem to get this to work with the code I presented in my original post. i can not hard code the date like you have, it is being pulled from the db (column name is "releaseDate"), and I was tryin to use PHP instead of a sqlserver funtion in the sql statement to format the date. Still struggling :(
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

Please change your code from

Code: Select all

<? $releasedate = getDateArray( $thispage["releasedate"] ); ?>
<?=$releasedate["month"]?>-<?=$releasedate["day"]?>-<?=$releasedate["year"]?>
to:

Code: Select all

<pre><?php
$releasedate = getDateArray( $thispage["releasedate"] );
echo $releasedate["month"], ' - ', $releasedate["day"], ' - ', $releasedate["year"];
?></pre>
This way we get a better idea of what data is being returned from the db.

Please take a note:
  • You don't need to enter-scape-reenter-scape-reenter php (<?php code?>HTML<?php code?>) to print things on output. Take a look to [php_man]strings[/php_man] reference.
  • Try and use long <?php ?> to enclose PHP code due to compability issues. Read this thread.
  • Always use identention to your code, it helps when trying to findo bugs and make your code readble and professional. Read the Chapter 4. Coding Standards of the PEAR Manual.
Well, please post the output of the above code and show us what you're getting from the db.

Best regards,
Scorphus.
tdelobe
Forum Commoner
Posts: 41
Joined: Thu Aug 07, 2003 2:28 pm
Location: washington, dc

Post by tdelobe »

thanks for the tips first off. secondly i made those changes and it returns the same thing as before. so for a news release that had a releaseDate column entry in the DB of 2003/09/03 (yyyy/mm/dd), it shows up like this.

09-30-2003
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

Take a look to this:

Code: Select all

<?php
echo date('l, F j, Y', strtotime('2003-09-30'));
?>
output:

Code: Select all

Tuesday, September 30, 2003
Take a look to [php_man]date[/php_man] and [php_man]strtotime[/php_man]() reference and change your script to fit your needs.

Cheers,
Scorphus.
tdelobe
Forum Commoner
Posts: 41
Joined: Thu Aug 07, 2003 2:28 pm
Location: washington, dc

Post by tdelobe »

Got it finally! Thanks man, I am still learning this stuff so it takes me a while sometimes.
Post Reply