Page 1 of 1

Date manipulation

Posted: Sat Sep 07, 2002 1:14 am
by ace2600
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

Posted: Sat Sep 07, 2002 1:51 am
by volka
1) mktime()-manual i.e. user contributed note starting with
the mktime() function paired with the strftime() function has a nice feature of being able to do date addition and subtraction!
2) yes
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

Posted: Sat Sep 07, 2002 9:52 am
by gite_ashish
hi,
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>";

?>
after explode u can format the date as u please.
above code can be converted into some *smart* date format function !

Posted: Sat Sep 07, 2002 10:13 am
by gite_ashish
hi,

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?
All this can be done very well in the mysql_query() itself. Lets take your example:

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.