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).
Related
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
I was trying to retrieve Google calendar events using PHP for some analysing purposes. I used singleEvents= True settings to expand recurring events as single instances. In order to avoid retrieving unlimited recurring events, I tried to set the event start upper bound to 2015-06-01.
Here is the code I was using:
$eventList = $cal->events->listEvents($tempCal["id"], array(maxResults =>5000,singleEvents => True,timeMax => '2015-06-01T00:00:00-04:00'));
But it seems the timeMax parameter does not work.
I tried other time format '2015-06-01T00:00:00Z' and it does not work either.
However, I tried the same string using web browser and it retrieved all events before 2015-06-01 using https://developers.google.com/google-apps/calendar/v3/reference/events/list
What was wrong with my timeMax format?
The Solution who worked for me (ordering and time-bounds):
/* retrieve events for today */
$start = date(DateTime::ATOM, mktime(0,0,0,date('m'), date('d'), date('Y')));
$end = date(DateTime::ATOM, mktime(23,59,59,date('m'), date('d'), date('Y')));
$eventOptions = array("orderBy"=>"startTime",
"singleEvents"=>true,
"timeMin"=>$start,
"timeMax"=>$end);
$events = $service->events->listEvents($calendar_id, $eventOptions);
I have a site with premium memberships, but PHP script is not working well..
I the table accounts I have a column called reset_timer(default value is 0). When someone purchase a new premium membership, PHP add with time(); function into the reset_timer.
Premium is 1 month yes and is working well, but the "countdown" script is not working, the script take only the first reset_timer value so for example if the user1 purchase premium membership today, after 5 days, he have 25 days left, but if somebody in another account purchase 1 month of premium, he have 25 days left too, because script only taking the first reset_timer value.. This is the code:
function time_reset()
{
$now = time();
DB::select('accounts');
$timer = DB::fetch_row();
$timer = $timer['reset_timer'];
$difference = ($now - $timer);
return (2629743 - $difference);
}
How I can do it for the script look in every account and reset timer for work correctly?
Thank you very much!
I am getting a Google Calendar XML Feed with the following code.
My problem is I have 'singleevents=true' set in the URL yet when I actually look at the feed it is showing 24 entries and only one of these is unique, whereas the rest are all a repeat of one event.
In my calendar I have 1 single event and 6 events which each recur weekly.
$api = new clApi('http://www.google.com/calendar/feeds/' . $calendarID .'/public/full?singleevents=true&min-start=' . $now . '&max-start=' .$next_week);
if ($feed = $api->parse()) {
foreach($feed->get('entry') as $entry) {
.....
Is there something I'm doing wrong here?
I think you have the syntax for the parameter backwards. According to the documentation at https://developers.google.com/google-apps/calendar/v2/reference#Parameters : "Valid values are true (expand recurring events) or false (leave recurring events represented as single events). Default is false."
I have tried to Google this question, but have had no luck, maybe due to the search string "Z" not being specific enough.
Background:
Using Google Calendar Zend gdata library, and have been using simple code to list events for a specific time period. In this case, 27-02-2012 to 03-03-2012.
I had a number of single events listed, with one recurring event, titled "Rob", mon-fri weekly.
I am using the basic code without any fancy additions. yet :)
Problem:
when I used the $query->setSingleEvents(TRUE) parameter, everything worked as expected, and showed the correct events.
When I talk about 'showing' events, im talking about the PHP page which is using Zend gdata to show calendar events.
In all situations, the Google Calendar GUI is showing the correct data (ie. The 'Rob' event is not shown, as it was deleted.
But when I set this to FALSE, the recurring event "Rob", should have start times that are meant to be grouped, but instead, a few extra random events are shown without a starttime, just the same title. Even once ive deleted the recurring event, it still shows in the data returned by gdata.
Interesting Observation
The EventID for the event that was deleted but is still shown on the page ends with "Z".
It has the usual eventID followed by: _20120302T030000Z
My Question
This event was deleted in Google Calendar GUI. Why is gdata still showing an event which is deleted, and what does the eventid ending with a timestamp and Z mean.
Resolution Attempts:
1. Tried to change it from recurring event to a normal event (removing the recurrance 'for all events in series'
No luck, still extra ones listed.
2. deleted the recurring event completely for all events in series.
This showed again the correct results in SingleEvents(TRUE), but in FALSE, the old 'Rob' event is still shown, when it has completely been deleted from the calendar.
Code:
function outputCalendarByDateRange($client, $startDate='2012-02-27',
$endDate='2012-03-03')
{
$gdataCal = new Zend_Gdata_Calendar($client);
$query = $gdataCal->newEventQuery();
$query->setUser('default');
$query->setVisibility('private');
$query->setProjection('full');
$query->setOrderby('starttime');
$query->setStartMin($startDate);
$query->setStartMax($endDate);
$query->setSingleEvents(FALSE);
$eventFeed = $gdataCal->getCalendarEventFeed($query);
foreach ($eventFeed as $event) {
echo "Title: " . $event->title->text . "<br />";
echo "Event ID: " . $event->id->text . "<br />";
foreach ($event->when as $when) {
echo "Start: " . $when->startTime . "<br />";
}
echo "<br />";
}
}
Here's what I found in the docs:
36.5.6. Deleting Events Calendar events can be deleted either by calling the calendar service's delete() method and providing the edit
URL of an event or by calling an existing event's own delete() method.
In either case, the deleted event will still show up on a user's
private event feed if an updateMin query parameter is provided.
Deleted events can be distinguished from regular events because they
will have their eventStatus property set to
"http://schemas.google.com/g/2005#event.canceled".
it looks like you'll have to test for event status when you parse your feed.
BTW the timestamp breaks down to
year:2012 day:03 month:02 time:T hours:minutes:seconds 03:00:00 zulu:Z
Zulu is the same as GMT (Greenwich Mean Time) or UTC (Universal Time Code)