1 month Timer for Premium Membership - php

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!

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

How to set cart expiration in woocommerce within 15 minutes?

I have a wordpress website that allows non logged-in customers to order with the code below
global $woocommerce;
$woocommerce->session->set_customer_session_cookie(true);
The issue that I am facing right now is that the products left unordered are left in cart because all non-logged-in custumers share the same woocommerce session.
I am thinking of setting an expiry time to the session or the cart so that it can clear products in cart.
The code that I saw in: Woocommerce Set Cart Expiration Interval returns 72 hours in seconds
add_filter('wc_session_expiring', 'filter_ExtendSessionExpiring' );
add_filter('wc_session_expiration' , 'filter_ExtendSessionExpired' );
function filter_ExtendSessionExpiring($seconds) {
return 60 * 60 * 71;
}
function filter_ExtendSessionExpired($seconds) {
return 60 * 60 * 72;
}
Do you know how can I make the filter return every 15 minutes?
Any idea is appreciated. Thanks...
Add the following to your themes functions.php file
add_filter('wc_session_expiring', 28800);
add_filter('wc_session_expiration' , 28800);
28800 is 8 hours in seconds so replace this with 900
If this code you linked is working just change the time calculation: 60 * 15.

PHP Server time is behind local time

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

Wordpress wp_schedule_event randomly between 30 and 60 minutes

Is is possible to start the WP-Cron randomly between 30 and 60 minutes?
What i have
add_action('my_hourly_event', 'do_this_hourly');
function my_activation()
{
if(!wp_next_scheduled( 'my_hourly_event' ))
{
wp_schedule_event( current_time( 'timestamp' ), 'hourly', 'my_hourly_event');
}
}
add_action('wp', 'my_activation');
function do_this_hourly()
{
// do something
}
Unfortunately the wp_schedule_event doesn't have 30 min and accepts only these intervals: hourly, twicedaily(12H), daily(24H).
In my opinion is a bit strange to have a scheduled event that can change randomly, and probably you should look at a different implementation.
Without discussing your choice I am going to provide a possible answer.
There are plugins with hooks into the Wordpress cron system to allow different time interval.
One solution is to set only one cron every 30 minutes and have a custom function that randomly will be executed or not.
if (rand(0,1)) { ....
For example:
after 30 min the function will be executed (and you have 30 min cron)
after another 30 min the function simply skip the run
for the next 30min will be triggered again and executed(and you have your 1 hour cron).
The problem there is to force the execution at 1 hour (after 1 skip), because you can end up to skip more than +30min. This can be achieved storing the value of the last execution.
Another solution is to have 2 cron (30 min and 1 hour) nearly in the same time and having a custom function that will trigger the 30 min if the 1 hour is not running and so on.
Here is a nice Wordpress cronjob plugin
If you need to store the cron execution safely in a Wordpress table you can use the Wordpress add_option function, with get_option and update_option to get and update its value.
In the code below, I'll be using activation hook instead of wp hook, feel free to use after_switch_theme if it's a theme your code resides in...
You can use wp_schedule_single_event() and simply add a single event to happen randomly between 30-60 minutes every time the event happens ;)
/**
* Registers single event to occur randomly in 30 to 60 minutes
* #action activation_hook
* #action my_awesome_event
*/
function register_event() {
$secs30to60min = rand( 1800, 3600 ); // Getting random number of seconds between 30 minutes and an hour
wp_schedule_single_event( time() + $secs30to60min, 'my_awesome_event' );
}
// Register activation hook to add the event
register_activation_hook( __FILE__, 'register_event' );
// In our awesome event we add a event to occcur randomly in 30-60 minutes again ;)
add_action( 'my_awesome_event', 'register_event' );
/**
* Does the stuff that needs to be done
* #action my_awesome_event
*/
function do_this_in_awesome_event() {
// do something
}
// Doing stuff in awesome event
add_action( 'my_awesome_event', 'do_this_in_awesome_event' );

In Tiki I have API to create/update page, time and date of creation does not display properly

I have Tiki set up with my php site.
I create/update page using my own Tiki API file but when I show version of page in history tab than creation date is not displayed. Only time is displayed.
This is my code for create page in Tiki.
require 'tiki-setup.php';
include_once("tiki-calendar_setup.php");
require_once 'lib/tikilibsohyper.php';
require_once ('lib/userslib.php');
include_once ('lib/wiki/histlib.php');
global $tikilib,$calendarlib,$userlib,$histlib;
$tikilib->create_page($pageName, $hits, $pageData, $lastModif, $comment, $username, $ip= '0:0:0:0', $description='' , $lang, $is_html = false, $hash=null, $wysiwyg=NULL, $wiki_authors_style='', $minor=0, $created='');
This is for update page
$tikilib->update_page($pageName, $pageData, $comment,$username, $ip, $description = '');
Now in Tiki history page (TAB) data display as follows:
Information Version Action
--------------------------------------------------------------
2013-03-04 09:47 by administrator 3 v s b
2013-03-04 09:47 by administrator 2 v s b
09:32 by administrator 1 v s b
First, why it displays only time?
I need both time and date.
You need to uncheck "Skip date for same day" on the General > Date and Time admin panel.

Categories