SitePoint Sponsor

User Tag List

Results 1 to 18 of 18
  1. #1
    SitePoint Guru
    Join Date
    May 2012
    Posts
    996
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post how to calculate the date

    Hi i am having problem on how can i calculate the 2 days before on the expected date...for example the expected date is july 25,how can i calculate the 2 days before july 25,because i want to try sending information to my client on before july 25 comes.


    Thank you in advance.

  2. #2
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,075
    Mentioned
    53 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jemz View Post
    Hi i am having problem on how can i calculate the 2 days before on the expected date...for example the expected date is july 25,how can i calculate the 2 days before july 25,because i want to try sending information to my client on before july 25 comes.


    Thank you in advance.
    Code PHP:
    $Date = new DateTime('2013-07-25');
    $Date->sub(new DateInterval('P2D'));
    print $Date->format('Y-m-d');

  3. #3
    SitePoint Guru
    Join Date
    May 2012
    Posts
    996
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by K. Wolfe View Post
    Code PHP:
    $Date = new DateTime('2013-07-25');
    $Date->sub(new DateInterval('P2D'));
    print $Date->format('Y-m-d');
    Hi K.Wolfe, Thank you for the quick reply what does it mean P2D ?

  4. #4
    SitePoint Member
    Join Date
    Jul 2013
    Location
    Nagpur
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello,
    Use this code
    $date = new DateTime();
    $date->sub(new DateInterval('P2D'));
    echo $date->format('Y-m-d') , PHP_EOL;

  5. #5
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,312
    Mentioned
    177 Post(s)
    Tagged
    8 Thread(s)
    Hi jemz,

    Quote Originally Posted by jemz View Post
    Hi K.Wolfe, Thank you for the quick reply what does it mean P2D ?
    It is an interval specification:
    The format starts with the letter P, for "period." Each duration period is represented by an integer value followed by a period designator.
    In this case P2D means a "Period of 2 Days"

    You can read more here: http://php.net/manual/en/dateinterval.construct.php

  6. #6
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,292
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Or you can do it with strtotime

    Code:
    $ts = strtotime('2013-07-01');
    $two_days_before_ts = strtotime('-2 days',$ts);
    $new_date = date('Y-m-d',$two_days_before_ts);

  7. #7
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,075
    Mentioned
    53 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by johnyboy View Post
    Or you can do it with strtotime

    Code:
    $ts = strtotime('2013-07-01');
    $two_days_before_ts = strtotime('-2 days',$ts);
    $new_date = date('Y-m-d',$two_days_before_ts);
    Yes, this will work too. OP had another thread regarding dates, and I was about to suggest moving to DateTime object, as it is newer and object oriented, allowing for IMO more functionality and cleaner code.

  8. #8
    SitePoint Guru
    Join Date
    May 2012
    Posts
    996
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post
    Hi jemz,



    It is an interval specification:
    The format starts with the letter P, for "period." Each duration period is represented by an integer value followed by a period designator.
    In this case P2D means a "Period of 2 Days"

    You can read more here: http://php.net/manual/en/dateinterval.construct.php


    Hi pullo, Thank you so much for enlighten my mind.

  9. #9
    SitePoint Guru
    Join Date
    May 2012
    Posts
    996
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @K. Wolfe ; Thank you again and to all who help...

    I tried to change to add 1 year please correct me if i am wrong

    Code:
    $Date = new DateTime('2013-07-25');
    $Date->add(new DateInterval('P1Y'));
    print $Date->format('Y-m-d');
    Last edited by cpradio; Jul 23, 2013 at 11:31.

  10. #10
    SitePoint Guru
    Join Date
    May 2012
    Posts
    996
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi pullo, Is it possible to get the system date from my desktop because i use this function date

    $date = date('m/d/Y h:i:s a');
    it does not coincide in my system date of my desktop.

    Thank you in advance.

  11. #11
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,312
    Mentioned
    177 Post(s)
    Tagged
    8 Thread(s)
    Hi jemz,

    AFAIK, you cannot use PHP to get the time on a client machine.
    This is because PHP is a server-side language that is executed before the code ever reaches the client's system.
    To get the client's time, you can use JavaScript.
    https://developer.mozilla.org/en-US/...l_Objects/Date

  12. #12
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,075
    Mentioned
    53 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post
    Hi jemz,

    AFAIK, you cannot use PHP to get the time on a client machine.
    This is because PHP is a server-side language that is executed before the code ever reaches the client's system.
    To get the client's time, you can use JavaScript.
    https://developer.mozilla.org/en-US/...l_Objects/Date
    Or get their timezone through other means, and calculate from there.

  13. #13
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,312
    Mentioned
    177 Post(s)
    Tagged
    8 Thread(s)
    Quote Originally Posted by K. Wolfe View Post
    Or get their timezone through other means, and calculate from there.
    Out of interest, how would you suggest doing that?

  14. #14
    SitePoint Mentor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,255
    Mentioned
    32 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Pullo View Post
    Out of interest, how would you suggest doing that?
    I guess it could be done by getting the user's location from their IP address, and then setting the timezone based on that, but it's not guaranteed to work as they could be using some kind of proxy or something like that.
    Last edited by fretburner; Jul 23, 2013 at 06:34. Reason: Corrected typo

  15. #15
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,312
    Mentioned
    177 Post(s)
    Tagged
    8 Thread(s)
    Yeah, I thought of that too, but given the IP lookup, coupled with the possibility of the user accessing the site via a proxy, it seemed like a lot of stress to do it this way.

  16. #16
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,075
    Mentioned
    53 Post(s)
    Tagged
    0 Thread(s)
    Depending on how crucial the timezone is to his application, I might try to use GeoIp to suggest their timezone to them, allow them to alter it and store it under their account preferences. From here all operations would be stored as UTC-0 and then you can display back to front end user based on their timezone setting.

  17. #17
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,075
    Mentioned
    53 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post
    Yeah, I thought of that too, but given the IP lookup, coupled with the possibility of the user accessing the site via a proxy, it seemed like a lot of stress to do it this way.
    You can always avoid this just by using UTC-0 as default for everything The only problem left is front end display to the end user, at that point it would just be on them to set their preference.

  18. #18
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,312
    Mentioned
    177 Post(s)
    Tagged
    8 Thread(s)
    Quote Originally Posted by K. Wolfe View Post
    You can always avoid this just by using UTC-0 as default for everything The only problem left is front end display to the end user, at that point it would just be on them to set their preference.
    That would be the way I would go.
    Much easier than doing an IP lookup to work out their time zone and gets around the proxy problem, too.
    As you say, depends how critical this is to the app in question.


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
  •