PHP Server time is behind local time - php

my server time is ahead of my local time (I cannot change my server time) by about 4 hours. The server is hosted in a different time zone, and changing the server time would have too many far reaching effects on too many other things.
Anyways, I have customers that place an order for a Gift Card for instance, and the gift card is dated to be sent out the same day that they ordered it, so the date stamp that gets placed on the Gift Card is (for instance), 1/3/2018.
But, let's say they placed the gift card order at 11:30 PM, and the server time has already moved onto 1/4/2018. My code below does not account for that:
$curDate = date('Y-m-d');
if ($card->getCardStatus() == 0){
if ((($card->getMailDeliveryDate() == null) || ($curDate == $card->getMailDeliveryDate())) && $card->getCardType() != 'offline') {
$card->setCardStatus(1);
$card->save();
$card->send();
}
}
Any ideas on how to take this scenario into account?
Thank you.
Edit Awh man, I got my question reversed around. My local time is ahead of my server time. I apologize. My Magento is set to eastern time (where I'm at) but the server itself is located in LA, thus PST. I don't want to change the timezone, I just want to make sure that orders placed in other time zones are accounted for.

This is where the PHP date_default_timezone_set() function steps in.
date_default_timezone_set — Sets the default timezone used by all date/time functions in a script
Set the default_timezone like this:
date_default_timezone_set('America/Los_Angeles');
More here http://php.net/date-default-timezone-set.
EDIT: As suggested by #RamKesavan, you can also set the timezone in Magento settings by doing this:
First set global timezone to GMT or UTC:
1.) Go to System -> Settings
2.) Edit your default scope
3.) Go to Configuration -> General -> General -> Locale Options
4.) Select GMT Standard Time (or UTC)
Then you need to set your default website scope to your preferred time zone like this:
1.) Go to System -> Settings
2.) Select the scope for each of your websites.
3.) Go to Configuration -> General -> General -> Locale Options again.
4.) Select W. European Standard Time (Europe/Berlin or Europe/Amsterdam) or select some other timezone appropriate for the location of your customers.
5.)Repeat for the next website.
The default website scope will ensure that your websites display Dutch, Chinese or American order and shipping times, or some other time according to your settings.
More here https://support.hypernode.com/magento-utc-hypernode.
EDIT 2: (OP changed the question)
It is impossible to detect the user date time locale settings with accuracy from the server side (PHP). This is what the client side (javascript) is used for.
There is a javascript script available jstimezonedetect which is used to easily detect the user time zone settings. Download the script and include it in the page where you have the purhase form.
To retrieve the timezone and send it with the purchase form you can add a hidden field inside the purchase form (it will be sent with the form) like this:
var tz = jstz.determine(); // call the jstimezonedetect script object
var tzName = tz.name(); // name of the user timezone 'Europe/Berlin'
var hiddenTimezone = document.getElementById('myHiddenTimezone');
hiddenTimezone.value = tzName; // add timezone value to hidden field
And in HTML you would need to have the hidden element added inside the purchase form like this:
<form ...>
// ...
// ...
<input type="hidden" id="myHiddenTimezone" name="userTimezone" value="" />
<!-- since the hidden element is inside the form and has a name -->
<!-- it will be sent with the form -->
// ...
</form>
On the server side (receiving side) you would get the timezone like this:
$userTimezone = $_POST['userTimezone']; // or $_GET['userTimezone'] if form method was GET
When a purchase occurs, you now have the server time and the user timezone. You can make 3 columns in your table. One for server time, another for user time zone and the third the current (calculated) user time.
You can then extract whichever column you wish and you would know for sure what you are getting from the database and how to write a PHP function to handle it.
EDIT 3:
To simply convert the a date from for example PST to EST you would do this:
$ESTDate = new DateTime('2017-02-04 12:32:43', new DateTimeZone('America/Los_Angeles'));
$ESTDate->setTimezone(new DateTimeZone('America/New_York'));
If you wish to echo the datetime with formatting:
echo $ESTDate->format('Y-m-d H:i:s');
If you wish to compare to another date you can use:
$difference = $ESTDate1->diff($ESTDate2);
which will produce results like this:
DateInterval Object
(
[y] => 3
[m] => 5
[d] => 15
[h] => 0
[i] => 0
[s] => 0
[invert] => 0
[days] => 1264
)
// usage
if($difference['days'] == 0)
// ...
Or you can use the > < and other operators like this:
// both dates must be in the same format (example: 'Y-m-d h:i:s')
if( $ESTDate1 > $ESTDate2) // date1 is greater
else // date2 is greater or equal

Related

Shopify Order API call Interval

I have integrated Shopify Order API. Reference Link
Currently fetching orders based on created_at_min field. I am calling API in every 10 min of interval like if last API called at 11:00 AM so the next API will fetch order whichever is created after 11:00AM and script will keep time of the current API call.
Timezone also set in my script code based on shopify timezone.
Issue is if order created at 11:00 AM and I am trying to fetch order which are created after 11:20 AM still 11:00 AM created orders are coming.
What should be the ideal time interval to call order API?
My code is below:
$lastScriptRun = $this->selectLastRun(); // Fetching last run time from database
// Here creating current time to store in database
$estDate = new DateTime( date("Y-m-d h:i:s") , new DateTimeZone('UTC'));
$estDate->setTimezone(new DateTimeZone('US/Pacific'));
$scriptStart = $estDate->format('c');
// Fetching orders
$orders = $shopify->Order->get( array('status' => 'any', 'created_at_min' => $lastScriptRun) );
....... // [after some code]
// Updating last API call time into database
$this->updateLastRun($scriptStart);
SDK Link
I found the solution.
This line was not returning UTC time properly.
$estDate = new DateTime( date("Y-m-d h:i:s") , new DateTimeZone('UTC'));
I tried with another one which found in defination and solution links.
$estDate = new DateTime( gmdate("Y-m-d\T00:00:00\Z") );
Solution: Ideal time can be anything (standard 30 min to call an API I set for my Application).

I am Facing issue in magento timezone when someone place order.

Timezone Issue is inside the order email.
I put My code which returns date like :
Code :
$created = Mage::helper('core')->formatDate($this->getCreatedAt(), 'long', true);
O/P :
March 31, 2016 9:10:33 AM EDT
But I want to Need Date Like Based on EST Timezone :
EST - Timezone with proper
So please any one can help me how can i achieve this and also first priority to magento code and second one PHP.
Thank You.
Login as admin and navigate to System -> Configuration -> General -> Locale Options -> Timezone and change timezone from there

Common issue with php timezone in Joomla 3

I have a custom .php file that creates new Joomla articles with the desirable content. So, in my php script I have a connection to database, to jos_content table in order to create new articles.
One of the columns in SQL query is "created" and the value is a php variable $d (it is date).
date_default_timezone_set("Europe/Belgrade");
$d = date('Y-m-d H:i:s');
In GLOBAL CONFIGURATION > SERVER in Joomla 3, I set up a timezone to be also Belgrade.
Additionaly, I created a php.ini file in my root directory on server and add line of code of the particluar timezone
date.timezone = "Europe/Belgrade"
And after all this effort, the timezone is not applying. It is +2 again and obviously I have to change somewhere else, something else.
If I set up H:i:s to be 00:00:00 I got the result that my article will be published on today's date but at 02:00:00.
What am I missing?

Best way of triggering to delete a row from database if 'date` is 'today'?

I made a Diary at http://kees.een-site-bouwen.nl/agenda which can have Events for specific dates. You can add an event using the form located here: http://kees.een-site-bouwen.nl/evenementform
As you can see I have 3 fields 'Datum, Van & Tot' (translation: Datum = date, Van = From, Tot = till)
If the time on that specific date expires I would like to run a script which deletes that specific row from the database.
I searched on google and found a few things like MYSQL Trigger and PHP cronjob, but I don't know if there's an easier way to do this? or how to do it exactly.
My database structure looks like this:
agenda // diary
- - - - // not added the whole structure.
idagenda
titel
waar
www
email
activated
....
....
agendadatum // diary dates
- - - - - -
id
datum
van
tot
idagenda
as you can see I'm using a join to add more dates to one event.
How could I trigger the datetime to delete the rows from the db if the date = today?
NOTE: I am using the Codeigniter framework.
You could set a hook. And use a function like
$this->db->where('tot <', date('Y-m-d H:i:s'));
$this->db->delete('agendadatum');
My codeigniter is a bit rusty but that query should remove all "old" entries on EVERY page load. So if you're going for high traffic this will not hold up.
Running a cronjob every hour/day is probably the "cleanest" way. This will require you to set a where condition on all selections of agendadatum that forces the "tot" date to be in the future. Else it's possible you see expired entries.
Edit:
From what I can gather if you define your hook like:
$hook['post_controller_constructor'] = array(
'class' => '',
'function' => 'delete_old_entries',
'filename' => 'agenda.php',
'filepath' => 'hooks',
'params' => array()
);
And create a file named agenda.php in application/hooks which looks like:
<?php
function delete_old_entries() {
$CI =& get_instance();
$CI->load->database();
$query = $CI->db->query(" DELETE FROM agendadatum WHERE tot < NOW(); ");
}
It should work. However this is pieced together from what I could find and untested. So it might work, it might not. But something along these lines should do the trick even if it isn't this.
If I understand correctly:
CREATE TRIGGER deleteRows AFTER UPDATE,INSERT ON myTable
FOR EACH ROW BEGIN
DELETE FROM myTable WHERE Datum = NOW()
END;
MySQL has a built-in Event Scheduler that basically allows you to run arbitrary SQL code at scheduled times. You could purge your database once a day for instance.
However, I know from your other question that you are hosting your project on a shared host, and, unfortunately, shuch hosts often disable the feature. Check out this manual page to find out whether the feature is active on your server.

javascript and php on the fly calendar

I have the code for a javascript calendar and it works perfectly as it creates it when the page loads. However I was wondering if it's possible to add events to it. I found a plugin (jQuery) that enables the user to hover over a td with class "event" and an event will be displayed. So since this calendar will not be used by me but by someone else who knows nothing about developing I was wondering if there is a way to make a php file or upload or something so she can upload the event. I mean, let's say she wants an event on the 3rd then she uploads a file php reads it and tells javascript to add the class "event" that date and jQuery does the rest. Is it possible? I can't even figure out how to do it and I really hope I explained myself. Here's my javascript btw.
function buildCal(){
var d = new Date();
var month = d.getMonth()+1;
var year = d.getFullYear();
var monthName=['Enero','Febrero','Marzo','Abril','Mayo','Junio','Julio','Agosto','Septiembre','Octubre','Noviembre','Diciembre'];
var daysInMonth=[31,0,31,30,31,30,31,31,30,31,30,31];
var objectDay = new Date(year, month-1, 1); //fix date bug when current day is 31st
objectDay.od=objectDay.getDay()+1; //fix date bug when current day is 31st
var todaydate=new Date()
var scanfortoday=(year==todaydate.getFullYear() && month==todaydate.getMonth()+1)? todaydate.getDate() : 0 //DD added
daysInMonth[1]=(((objectDay.getFullYear()%100!=0)&&(objectDay.getFullYear()%4==0))||(objectDay.getFullYear()%400==0))?29:28;
var t='<div class="main"><table class="main" cols="7" cellpadding="0" border="0" cellspacing="0">';
t+='<h3 class="monthCSS" align="center">'+monthName[month-1]+' - '+year+'</h3><tr align="center">';
for(s=0;s<7;s++)t+='<td class="daysofweek">'+"DoLuMaMiJuViSa".substr(s*2,2)+'</td>';
t+='</tr><tr align="center">';
for(i=2;i<=42;i++){
var x=((i-objectDay.od>=0)&&(i-objectDay.od<daysInMonth[month-1]))? i-objectDay.od+1 : ' ';
if (x==scanfortoday)
x='<td class="today">'+x+'</td>'
t+='<td class="days">'+x+'</td>';
if(((i)%7==0)&&(i<36))t+='</tr><tr align="center">';
}
return t+='</tr></table></div>';
}
Something else, as you can see here, it adds blankspaces until it gets to an actual date. I was trying to make it check if(x was not a number) then add a td class="padding" however to do this I was trying to use x.match(/[0-9]+/) but it didn't seem to work and it would also be the first time I try to use regex with javascript would anyone know why is that wrong? or how to actually check for it?
Edit
Something odd is happening with this script and I don't know why, I tried to change from
t+='<td class="days">'+x+'</td>';
to
t+='<td class="days' + x +'">'+x+'</td>';
this, so I could select each td, but when I do this a new td is generated which contains
<td id="days<td class=" today="">1</td>
I have NO idea why this happens, I just know it is messing with the code because afterwards I get a "> printed (because of quotes mis-match caused by this new td...why is this happening?
The calendar systems I've created use a full php array of the month. so that you can iterate over it and for every corresponding blank day table cell there is a blank array for the day.
e.g.
$calendar_dates = array(
[week_1] = array(
[sun] = Null
[mon] = NULL
[tue] = array(
[events] = array(
event_id => 'event name'
event_name => ''
event_time => ''
)
[wed]
...
)
[week_1] => array()
...........
)
I build the days array by just creating an array from the specified date and current week
then I hit the databse to get events in that range
then cycle through the events and attatch them to the calendar array.
works like charm.
To get it to work with javascript just have it echo some specific javascript in the head of the html file that control the opening and closing of the calendar days.
give you client a simple login page to input/edit events in a webform.
It sounds like you're wanting to push event data from the server to your webpage containing the calendar. While this is possible, it's difficult and generally not worth the effort. You would be better off building some AJAX into your calendar and polling the server for event updates every 5 or 10 minutes or so. This will introduce some delay between when new events are uploaded and when they display on the calendar, but will be much easier to develop.

Categories