How to Substract time from datetime - php

I am using a timezone based script where there is deadline based on timezones. Say for example a deadline is on 7th July at 6PM for a person in IST timezone. The deadline should be 1:30 less than 6pm in Dubai as per their timezone.
I have already calculated the difference between the two timezone difference. I am stuck at deducting that calculated time from the deadline time.
I have saved the timezones in +5:40 +4:00 -4:00 this format instead of using php default ones.

Here's what I use to add and subtract time from a date.
First of all, I get the date from the database and then convert it to a DateTime object.
$date = new DateTime($date);
I use add and a DateInterval to add time. Here's an example to add hours.
$date->add(new DateInterval("PT{$hoursToAdd}H"));
And here's and example to subtract hours intead:
$date->sub(new DateInterval("PT{$hoursToSubstract}H"));
Check this out to know how to work with DateInterval and add/subtract different times: http://php.net/manual/en/class.dateinterval.php

Can you explain a little more specifically what you want to do?
You want to subtract when data already on some var inside your script or you want the SQL query needed ?
If so maybe you can use this answer ---> How to subtract 3 hours from a datetime in MySQL?
The last answer of this post maybe is the one that fit the most what you need :
Assuming you have some timezone issue and know source and destination timezone, you could convert it like so
SELECT
DATE_FORMAT(CONVERT_TZ(x.date_entered, 'UTC', 'Europe/Berlin'), '%Y-%m-%d') AS date
FROM
x
ORDER BY
date ASC;

Related

Is PHP DateTime object bugged?

I want to increment Timestamp to generate dates (timestamps being assumed with time=0 hour, 0 minute, 0 second). All worked fine along my loop unless the day of 2016-10-30: Example follows:
$date1=new DateTime;
$date1->setDate(2016,10,30);
$date1->setTime(0,0,0);
$date1_str=$date1->format('Y-m-d H:i:s');
$timestamp1=$date1->getTimestamp();
$timestamp2=$timestamp1+(60*60*24);// create the next day...
$date2=new DateTime;
$date2->setTimestamp($timestamp2);
$date2_str=$date2->format('Y-m-d H:i:s');
echo "date1_str=$date1_str; date2_str=$date2_str<br>";
The result I get is the following:
date1_str=2016-10-30 00:00:00; date2_str=2016-10-30 23:00:00
The 24 hours increment of timestamp is seen as a 23 hours increment!!
The remaing of the loop keep locked to time 23:00:00 instead of 00:00:00
When you play with time, and especially with dates you have to have in mind DST (Daylight Saving Time) for the date you referred 30/10/2016 the time changes for some countries in the world, so you have to take it into consideration when you apply your time local settings.
I think that you should use date_default_timezone_set in the top of your instructions. you can see more information in : http://php.net/manual/en/datetime.settimestamp.php.
keep in your mind that you will get varying results dependent on your current timezone. for example when you use date_default_timezone_set('America/New_York'); you will get : date1_str=2016-10-30 00:00:00; date2_str=2016-10-31 00:00:00
i hop this can help you

Finding the difference between days

I'm having a table as follows to store future date,
email date
abc#gmail.com 8/10/2014
and I want to do is to find the difference between the above date and the sever date.
I'm using date("m/d/Y") to get the current date.
If date("m/d/Y") = 07/20/2014, then I need the answer as 21.
Please help me find the difference between those days using PHP & MySQL, or suggest a better way to find the difference in days.
You can convert date to timestamp then calculate the days:
(int)(strtotime('8/10/2014')-strtotime('07/20/2014'))/60/60/24
The easiest way would be to store your dates as timestamps, then you could subtract the current timestamp with the one you've saved in a database.
The PHP function time() returns the current timestamp – that is the number of seconds since the 1st of January 1970. You can then format a timestamp $stamp to your liking with date('m/d/Y', $stamp), for example.
Aside from facilitating arithmetic operations with dates, you can display more or less information with timestamps by formatting them with date(), as well as show different formats (July 13, 2014 vs. 07/13/2014). If you save a date as a string, e.g. "8/10/2014", it will be very complicated for you to change the format, and it won't be possible to get the correct time, for example.
Finding how long ago in days a timestamp $stamp was to the current time is very easy:
$now = time();
$days = ($stamp - $now) / (24*3600);
Use round() to get a full number if desired (e.g. 7.2309 would simply become 7).
I'd personally do it using DateTime:
Select your date out into a variable. In this instance We'll call it $DBDate
$DBDateObj = DateTime::createFromFormat('j/m/Y', $DBDate);
$TodayDateObj = new DateTime();
$interval = $TodayDateObj->diff($DBDateObj);
$daysDiff = $interval->days;
In $daysDiff you should now have the difference in days.

Get date object from difference between 2 dates, timezone dependant

I have a date stored in a database with timezone UTC, the date is a few days/hours/minutes ahead of right now.
I then need to calculate the difference in time between right now and the given date, bearing in mind that there could be different timezones at play.
I have been doing the following but it is wrong as the timezone isn't taken into consideration.
$interval = date_create('now')->diff(date_create($listing_end_date));
//$interval->d = days
//$interval->h = hours
//$interval->i = minutes
How can I do the above with the timezone taken into account?
You need to pass a second argument to date_create telling it what time zone is represented by the date in $listing_end_date. For example, assuming the string is in UTC:
$interval = date_create('now')->diff(date_create($listing_end_date,
new DateTimeZone('UTC')));

What are the pitfalls in the approach to calculate time diff

I wish to calculate the difference b/w 2 times in min:sec format . so is my approch correct
date("i:s",(strtotime($User['end_time']) - strtotime($User['start_time'])));
You may get the problems with timezones on some servers.
A bettter way would be using UTC timezone for calculation:
$date = new DateTime('', new DateTimeZone('UTC'));
$date->setTimestamp(strtotime($User['end_time']) - strtotime($User['start_time']));
echo $date->format('i:s');
Another thing, if they are different in exactly 1 hour, the result will be 00:00
strtotime($User['end_time']) - strtotime($User['start_time']) gives you the difference in seconds. Then you pass it to date, so you get the minute and second of the date whose unix timestamp is that.

generate unix timestamp from last time

suppose i have one column in mysql database which is stated last time one equipment is up in h:i:s format (ex: 00:05:11) or 1d21h, means that the equipment is on since 5 min before, what is the best method i can convert this to unix timestamp, say if using php script how? or direct convert last unix timestamp using mysql function query.
actually, i want to calculate for start time for this equipment uptime in unix timestamp where i have one column in mysql startcapture that will be deducted with last column to get start time. so starttime = startcapture - last (this last time that has to convert to unix timestamp based on now() - h:i:s ). but the problem is sometimes the format change from h:i:s to ex: 1d22h, if h:i:s means the equipment is up since ex: 00:05:11 min before and if 1d22h means the equipment already up 1 day 22 hours before.
so the main things here is to convert last column to appropriate unix timestamp.
please help guys, asap.
update:
simplified like this,
i want to display the start time, that can only be calculate by deducting startcapture column with last column,
startcapture column in unix timestamp, while the last column in h:i:s format sometimes in 1d22h format (means the equipment is already up since 1 day 22 hour or since 00:05:11, hours minute seconds)
the only things I want is to convert last column into unix timestamp which is time() - (00:05:11 or 1d22h),
is there any specific function?
If you have the time parsed into variables then you can use mktime to get the timestamp:
http://www.php.net/manual/en/function.mktime.php

Categories