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.


$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 ?

Hello,
Use this code
$date = new DateTime();
$date->sub(new DateInterval(‘P2D’));
echo $date->format(‘Y-m-d’) , PHP_EOL;

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

Or you can do it with strtotime

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

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

@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


$Date = new DateTime('2013-07-25');
$Date->add(new DateInterval('P1Y'));
print $Date->format('Y-m-d');

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.

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.

Or get their timezone through other means, and calculate from there.

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.

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. :slight_smile:

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.

You can always avoid this just by using UTC-0 as default for everything :slight_smile: 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.