Page 1 of 1
PHP and Dates
Posted: Mon Nov 17, 2003 8:14 am
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ї"releasedate"] ); ?>
<?=$releasedateї"month"]?>-<?=$releasedateї"day"]?>-<?=$releasedateї"year"]?>
Any help would be greatly appreciated. Thanks.
quick clarification
Posted: Mon Nov 17, 2003 8:16 am
by tdelobe
The date should look like this...
Wednesday, January 1, 2003
Posted: Mon Nov 17, 2003 8:35 am
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
Posted: Mon Nov 17, 2003 8:37 am
by tdelobe
i am using a SQLServer DB.
Posted: Mon Nov 17, 2003 8:49 am
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
Posted: Mon Nov 17, 2003 9:42 am
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

Posted: Mon Nov 17, 2003 11:44 am
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.
Posted: Mon Nov 17, 2003 11:57 am
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
Posted: Mon Nov 17, 2003 12:08 pm
by scorphus
Take a look to this:
Code: Select all
<?php
echo date('l, F j, Y', strtotime('2003-09-30'));
?>
output:
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.
Posted: Mon Nov 17, 2003 12:39 pm
by tdelobe
Got it finally! Thanks man, I am still learning this stuff so it takes me a while sometimes.