Date manipulation

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
ace2600
Forum Commoner
Posts: 30
Joined: Fri Jun 21, 2002 12:12 am

Date manipulation

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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
User avatar
gite_ashish
Forum Contributor
Posts: 118
Joined: Sat Aug 31, 2002 11:38 am
Location: India

Post 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 !
User avatar
gite_ashish
Forum Contributor
Posts: 118
Joined: Sat Aug 31, 2002 11:38 am
Location: India

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