# Thread: how to calculate the date

1. ## 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.

2. Originally Posted by jemz
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.

Code PHP:
```\$Date = new DateTime('2013-07-25');
\$Date->sub(new DateInterval('P2D'));
print \$Date->format('Y-m-d');```

3. Originally Posted by K. Wolfe
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. Hello,
Use this code
\$date = new DateTime();
\$date->sub(new DateInterval('P2D'));
echo \$date->format('Y-m-d') , PHP_EOL;

5. Hi jemz,

Originally Posted by jemz
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. 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. Originally Posted by johnyboy
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. Originally Posted by Pullo
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. @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');
print \$Date->format('Y-m-d');```

10. 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.

11. 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. Originally Posted by Pullo
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. Originally Posted by K. Wolfe
Or get their timezone through other means, and calculate from there.
Out of interest, how would you suggest doing that?

14. Originally Posted by Pullo
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.

15. 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. 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. Originally Posted by Pullo
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. Originally Posted by K. Wolfe
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.

#### Posting Permissions

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