SitePoint Sponsor

User Tag List

Results 1 to 18 of 18
  1. #1
    SitePoint Addict anita_86's Avatar
    Join Date
    Aug 2010
    Location
    Nagpur, India
    Posts
    232
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    1 Year adding in date

    Hi. I wanted to know the simplest way to show reminder after one year of today's date. Like if I purchased a product on 12/12/2011, the reminder will be shown on 12/12/2011. Similarly how can I add 18 days in today's date?Can anyone help me out in this regard?
    It's easy once you know how...

  2. #2
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by anita_86 View Post
    Similarly how can I add 18 days in today's date?Can anyone help me out in this regard?
    PHP Code:
    echo date('d/m/Y'strtotime('+18 day')); 
    To add one year to a date you could do something like:

    PHP Code:
    $date '05/06/2011';
    $date strtotime($date);
    $new_date strtotime('+ 1 year'$date);
    echo 
    date('d/m/Y'$new_date); 

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    If the date is kept in a mysql database, as say, purchase_date:
    Code:
    purchases
    ========
    id | 23
    name | "Nick Clegg" 
    purchase_date | "2010-12-12"
    title | "Supporting the Euro"
    // etc
    find purchases from exactly a year ago today:

    Code:
    "Select id, name from purchases where purchase_date = DATE_SUB(CURRENT_DATE(), INTERVAL 1 YEAR)";
    This is untested, and there are other ways to acheive the same, take a look at the Mysql manual page on date manipulation.

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    @Immerse -- certainly in the UK (or is it europe?), this strtotime() behaviour always worries me, how does it know you mean 5th June and not 6th May in your input?

    PHP Code:
    $date '05/06/2011'
    $date strtotime($date); 
    $new_date strtotime('+ 1 year'$date); 
    echo 
    date('d/m/Y'$new_date); 
    Gives 06/05/2012

    The last line suggests you meant 5th June, but what you get back is 6th May.

    This is no doubt true of the whole DateTime class too, which IIRC is built upon strtotime().

    EDIT

    Its not the fault of strtotime, btw, but is something we have to watch out for.

  5. #5
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    To be honest, I generally use strtotime() on dates from the database, which works OK (Y-m-d format).
    But for European/ UK dates, good point

  6. #6
    SitePoint Addict anita_86's Avatar
    Join Date
    Aug 2010
    Location
    Nagpur, India
    Posts
    232
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thx a lot Immerse and Cups.I will go through both the methods and will let u know.
    It's easy once you know how...

  7. #7
    SitePoint Addict anita_86's Avatar
    Join Date
    Aug 2010
    Location
    Nagpur, India
    Posts
    232
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have got desired result using both the methods.Thanks for ur valuable replies.
    Quote Originally Posted by Immerse View Post
    PHP Code:
    echo date('d/m/Y'strtotime('+18 day')); 
    This worked like magic for me:
    PHP Code:
    $new_date strtotime('+ 18 day'$date);
    echo 
    date('d/m/Y'$new_date); 
    It's easy once you know how...

  8. #8
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    My solution was designed to help you understand how you might select matching dates from your mysql database.

    Anyhow, glad you found out how to do what you wanted.

  9. #9
    SitePoint Addict anita_86's Avatar
    Join Date
    Aug 2010
    Location
    Nagpur, India
    Posts
    232
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well how can i design my query to find out the reminders which are coming only after current date? like "where purchase date > current date"??
    It's easy once you know how...

  10. #10
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    "Select id, name from purchases where purchase_date = CURRENT_DATE()";

  11. #11
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    "Select id, name from purchases where purchase_date = CURRENT_DATE()";
    In addition:
    If your date filed is 'datetime' instead of only type of 'date' then you must format the date for the field:
    Code mysql:
    SELECT id, name FROM purchases WHERE DATE_FORMAT(purchase_date, '%Y-%m-%d')=CURRENT_DATE;

    Otherwise you will not get desired results!
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  12. #12
    SitePoint Addict anita_86's Avatar
    Join Date
    Aug 2010
    Location
    Nagpur, India
    Posts
    232
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok.got it.never thought it can be so easy
    It's easy once you know how...

  13. #13
    SitePoint Addict anita_86's Avatar
    Join Date
    Aug 2010
    Location
    Nagpur, India
    Posts
    232
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi again.Though the matter is solved, but it cant display the desired result in reminder section
    Is this possible to do this through query?

    PHP Code:
    WHERE  sales.inv_date CURRENT_DATE 18 days 
    And
    PHP Code:
    WHERE  sales.inv_date CURRENT_DATE 1 year 
    This way when the date after 1 year or 18 days will come, it will display today's data.
    It's easy once you know how...

  14. #14
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Look at my previous reply at #3 above

  15. #15
    SitePoint Addict anita_86's Avatar
    Join Date
    Aug 2010
    Location
    Nagpur, India
    Posts
    232
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    Look at my previous reply at #3 above
    That worked well.
    Just curious to know is this syntax correct?
    PHP Code:
    sales.inv_date DATE_SUB(CURRENT_DATE(), INTERVAL 18 DAY
    It's easy once you know how...

  16. #16
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,862
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by anita_86 View Post
    Just curious to know is this syntax correct?
    PHP Code:
    sales.inv_date DATE_SUB(CURRENT_DATE(), INTERVAL 18 DAY
    Yes - unless you want to shorten CURRENT_DATE() to CURDATE() which is a shorter alternative way of referencing the current date.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  17. #17
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,323
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    if it's short you want, change this --
    Code:
    sales.inv_date = DATE_SUB(CURRENT_DATE(), INTERVAL 18 DAY)
    to this --
    Code:
    sales.inv_date = CURRENT_DATE - INTERVAL 18 DAY
    i don't like using CURDATE() in place of CURRENT_DATE because the latter is standard SQL
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  18. #18
    SitePoint Addict anita_86's Avatar
    Join Date
    Aug 2010
    Location
    Nagpur, India
    Posts
    232
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot.It was very helpful.
    It's easy once you know how...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •