Adding timezone and minutes to a date/time received from multiple parameters - php

I'm making a mail system application, in which the user selects the date and time to send the email one.
He selects the date from a jQuery datepicker, then hours from a drop down ranging from 01-24 and then minutes from a drop down ranging from 01-60.
Finally, I make a date with this setting :-
$time = $_POST['hours'] . ":" . $_POST['minutes'];
and date my
$_POST['datepicker']
But here is the deal, I need to add the timezone to the hours as well, and the date should be settled accordingly to the time we get after adding the timezone. Suppose we select 23:50 and add +3:30 then the date will be changed as well.
I tried to do it with simple calculations, but results were stupid.
I also need to add 5 minutes to the minutes, so suppose the input is 23:56 and time zone +3:30, then 56+5 = 01 (next day) and then the timezone is added as well.
I have timezone likes this
<select name="timezone">
<option value="1:30">1:30</option>
<option value="-5:30">-5:30</option>
What would be the best way to get the required time with the correct date?
Any suggestion is helpful.

Related

PHP date time conversion issues related to daylight savings

I have some localisation functions within my webapp (exposed via Ajax) which enable users to display some datetime information in their own timezone as well as the timezone relating to the location of the entity the data primarily relates to.
As a result of some testing, I've discovered that whilst my code is capable of converting DB sourced date/time values accurately (i.e. including DST adjustments), the code would not calculate the correct offsets when I was using dates provided within a form. The best I've been able to achieve is converting the form date/time value to UTC but the incorrect offset is applied. This means that I need to check whether the date falls within the daylight savings range and if so, pull the datetime value back by an hour. This seems like an ugly fix and I'd appreciate it if anyone knows where I've gone wrong:
Example:
Input value of 25/11/2018 16:00 NZDT (Pacific/Auckland) = UTC+13
Output value should be 25/11/2018 14:00 ADT (Australia/Sydney) = UTC+11
If I extract the source value from the Database and then convert then the calculations work fine, no issues
If I use a form provided value (which I need to check for in case the user has updated the value without saving it), then the following occurs:
FROM DB1 = 2018-11-25 03:00:00 (value in the DB, in UTC)
FROM FORM1 = 25/11/2018 16:00 (raw value from the form)
FROM FORM2 = 2018-11-25 04:00:00 (changes to UTC-12, i.e. NZST) which is wrong, should be the same value as DB1, i.e. UTC-13 which is the offset from NZDT back to UTC)
FROM FORM3 = 2018-11-25 03:00:00 (corrected, once I check whether date/time value falls within a daylight savings range)
Code follows:
echo "FROM DB1: ".$getListing['lo_deadline']."<BR />";
echo "FROM FORM1:".$in_lo_deadline."<BR />";
$frmDateTime = DateTime::createFromFormat($this->view->user->user_datetimeformat, $in_lo_deadline,new DateTimeZone($list_tmzn));
echo "FROM FORM2:".$frmDateTime->format('Y-m-d h:i:s')."<BR />";
//If $frmDateTime is in daylight savings then subtract an hour to arrive at the correct UTC value
if ($frmDateTime->format('I'))
{
date_sub($frmDateTime, date_interval_create_from_date_string('1 hours'));
}
echo "FROM FORM3:".$frmDateTime->format('Y-m-d h:i:s')."<BR />";
$dates['set_dead'] = convertUserDate($frmDateTime->format('Y-m-d h:i:s'), $this->view->user->user_datetimeformat, $user_tmzn,'UTC');
What I understand from your code, the reason why it's showing 2018-11-25 04:00:00 is because of AM/PM format. You should be using capital H instead of h in your code.
You need to understand the difference between 24 hour and 12 hour formats. 2018-11-25 04:00:00 according to your format is in AM/PM or 12 hour format, but in 24 hour format it should be 2018-11-25 16:00:00. Database time is always in 24 hour format.
Change
$frmDateTime->format('Y-m-d h:i:s')
to
$frmDateTime->format('Y-m-d H:i:s')
Note: You don't need to manually subtract an hour to calculate daylight saving time as php does it automatically.

Some questions about EDT and time difference

Introduction to my website
My website is for visitors in Korea(AKA Republic of Korea).
And the server for My website is in the United States of America.
And PHPMyAdmin displays EDT when it runs a query SELECT ## system_time_zone.
Structure of my website
When I first uploaded my website to this server in October this year, I checked the DB time.
And it seemed that there was a time difference of 13 hours with Korea. So I added 3600 * 13 seconds to DB time(without setting timezone) as follows.
const Offset = 3600 * 13;
$SelectNow = $PDO->prepare('SELECT DATE_ADD(NOW(), INTERVAL '.Offset.' SECOND)');
$SelectNow->execute() or exit;
$DbNow = $SelectNow->fetchColumn();
My website takes $DbNow as above and uses it in various situations.
For example, in the posting situation, enter $DbNow in the datetime field of the INSERT INTO query as follows:
$WriteNote = $PDO->prepare('INSERT INTO table_note(my_datetime, my_contents) VALUES("'.$DbNow.'", :my_contents)');
$WriteNote->bindValue(':my_contents', $my_contents, PDO::PARAM_STR);
$WriteNote->execute();
The problem situation
One day in November of this year, when I wrote a post and checked the date field(my_datetime) of the post, I got an additional time difference of one hour with Korea.
Apparently, at the end of October, I corrected the time difference of 3600 * 13. And then I confirmed that it matches the Korean time. However, in November, There is a time difference of one hour!
Guess the cause
It seems that US summer time is being applied to the DB server of my website. Did I guess right?
My question
1) How can I solve this time difference fundamentally?
Is it correct to convert DB time to KST?
Or is it the correct way to convert to UTC and then added 3600 * x to UTC?
2) Even though the problem is resolved, some of the existing data in my DB has a time difference of one hour with Korean time.
What query do I use if I want to select the data with a time difference?
And how much more or subtract it from the data to get rid of the 1 hour time difference?
Use UTC to store time in Database.
change your queries to insert with UTC datetimes.
Use external libraries to convert UTC to respective timezones.
(below are the my personal recommendation.)
There may be best of it.
PHP : Carbon
Javascript : Moment, moment timezone.
No, it takes timezone of Database server resides in.
little manual verification, or create a job to change all dates in UTC.
Edit:
http://carbon.nesbot.com/docs/
I mean you can create a script and run with cron job.

PHP How to adjust availability timezones

When a user signs up for my site they enter their weekly availability in this format:
Monday 1300 to 1400
Monday 2100 to 2200
Tuesday 1200 to 1300
Tuesday 1400 to 1500
Etc.
They also indicate their timezone based on a select menu, such as America/Los_Angeles.
What I want to happen is for those times to be adjusted based on the timezones of users that visit their profiles who have different timezones set. What is the best solution for this?
Store their availability as seconds from the EPOCH, and then convert those to proper times for users based on their time zone.
Store the difference between UTC and selected timezone in value of field
<option value="3">UTC+3 timezone</option>
<option value="-5">UTC-5 timezone</option>
then add or cut it from to the output of hours.
If such difference provide negative result or more than 24 hours - set next or previous day of the week.
I think you don't realy need unixtime for such intervals.
I actually ended up doing this:
<?php while ($ava = mysql_fetch_array($avail_action)) {
date_default_timezone_set($profile_owners_timezone);
$timestamp_from = strtotime($ava['time_from']);
$timestamp_to = strtotime($ava['time_to']);
date_default_timezone_set($_SESSION['timezone']);
$time_from = date("g:i A", $timestamp_from);
$time_to = date("g:i A", $timestamp_to); ?>
Basically I got the profile "owner's" timezone that he/she entered, set timezone to that, then set the timezone to "viewer's" timezone and set the variable to that.
Thanks all for the help!

datetime goes one hour forward when using unix timestamp with localtime settings

Firstly i want to tell my server time setting : its +3 Europe/Istanbul
and in php i am using
date_default_timezone_set('Europe/Istanbul');
I use datetime type in my mysql table, when i insert row, its written like 2011-10-01 15:16:09 its correct no problem but in php side when i query and echo date time with
echo strftime("%d %b %Y, %a %H:%M",strtotime(date("m/d/Y H:i",$data['UNIX_TIMESTAMP(date)'])));
i get time one hour forward like 16:16:09
i dont understand how to figure it out. Any idea ?
Edit :
when inserting the rows i give the date to mysql, it doesnt use internal date info like CURRENT_TIMESTAMP
The problem is almost certainly due to daylight savings times. If one part of the code specifies +3, that's already corrected for daylight savings. If the other part of the code doesn't recognize the date as already correct for daylight savings time, it will add an hour.
I know it's not a great answer, but I don't seem to be able to post a comment yet.

MySQL time/date calculation

First, an entry in the database:
I have an input form that writes start date, start and end times (in hour and minute) of working days plus lunch break in minutes (in the example dato=date, modetime=start hour, modeminut=start minute, fyrtime=end hour, fyrminut=end minute). I need to do several calculations:
First calculate the date, start hour and minute into the datetime field modetid.
The do a similar calculation with the end hours and minutes, but move the date up one day if end hours is less than start hour (lets call it fyrtid)
And finally calculate the difference between fyrtid and modetid minus the pause minutes.
Can it be done directly and automatically in the database (if yes, how) or do I need some PHP to do it (and again, if yes, how)?
I know its a tall order but I have not been able to find much information on date/time calculations that made much sense on my low level of knowledge. Any link to a comprehensive guide on date/time calculation in MySQL or PHP would also be greatly welcomed.
I suggest you to work by php function time() it's based on unix timestamp ( like UNIX_TIMESTAMP() in Mysql ) unix time is like this : 1307387678.
Use a calender in your form for start time also for your end time.
put a facility what clients could select time of day ( hour and minutes ) then covert those fileds by strtotime() to unix timestamp like following code ( set date format to mm/dd/yyyy ) :
$startdate = strtotime ( "$_POST['calender_start'] $_POST['hour_start']:$_POST['minutes_start']" );
$enddate = strtotime ( "$_POST['calender_end'] $_POST['hour_end']:$_POST['minutes_end']" );
Change your db table fields to : cpr,startDate,endDate,pause,basked,.....
it's so flexible.
while you want to fetch special recorde you could fetch rows by this sql :
SELECT ...... FROM [TABLE_NAME] WHERE [VALUE] BETWEEN startDate AND endDate
I hope it be usefull for you

Categories