These questions have prob been asked b4, but I couldnt find them when I searched.
I have a datestamp (yyyy-mm-dd) and store it in a db.
1)How can I add 3 months to the variable after I retrieve it from the db?
2)Does php automatically know to go to the next month if I add more days then are in the month?
3)And finally, can I echo the variable in a different format, say dd-mm-yyyy?
Thanks,
Ace
Date manipulation
Moderator: General Moderators
1) mktime()-manual i.e. user contributed note starting with
3) strftime()-manual
as mentioned somewhere else today try the receive the appropriate date-format from the database , i.e. by mySQL Date and Time Functions
2) yesthe mktime() function paired with the strftime() function has a nice feature of being able to do date addition and subtraction!
3) strftime()-manual
as mentioned somewhere else today try the receive the appropriate date-format from the database , i.e. by mySQL Date and Time Functions
- gite_ashish
- Forum Contributor
- Posts: 118
- Joined: Sat Aug 31, 2002 11:38 am
- Location: India
hi,
after explode u can format the date as u please.
above code can be converted into some *smart* date format function !
3)And finally, can I echo the variable in a different format, say dd-mm-yyyy?
Code: Select all
<?php
// date now in format: yyyy-mm-dd
$date = '2002-09-07';
echo "date: $date<P>";
$date = explode( '-', $date );
// date now in format: dd-mm-yyyy
$date = $dateї2] . '-' . $dateї1] . '-' . $dateї0];
echo "date: $date<P>";
?>above code can be converted into some *smart* date format function !
- gite_ashish
- Forum Contributor
- Posts: 118
- Joined: Sat Aug 31, 2002 11:38 am
- Location: India
hi,
For further details regarding mysql's date, time functions:
http://www.mysql.com/doc/en/Date_and_time_functions.html
MySQL is having bunch of functions for date, time.
I know u asked for php level code for doing above stuff... but this is one approach on MySQL level.
All this can be done very well in the mysql_query() itself. Lets take your example:1)How can I add 3 months to the variable after I retrieve it from the db?
2)Does php automatically know to go to the next month if I add more days then are in the month?
Code: Select all
<?php
mysql_query( 'SELECT date_add( "2002-09-07", INTERVAL 3 MONTH )' );
?>For further details regarding mysql's date, time functions:
http://www.mysql.com/doc/en/Date_and_time_functions.html
MySQL is having bunch of functions for date, time.
I know u asked for php level code for doing above stuff... but this is one approach on MySQL level.