Saturday, June 12, 2021

PHP: Calculating past and future dates

 The strtotime function in PHP is incredibly powerful but very much underutilised - mainly because some aspects can be a bit confusing and people are more confident 'doing things the hard way'. The strtotime function doesn't just convert date strings to timestamp values but also has its own 'language' to let you specify just about any date or time you want.

1. Calculating dates for past and future days

The easiest and most common calculations are based on seconds, minutes, hours and days. Months and years are also valid input but not always useful in calculations as they're not all of equal length because of the way our calendar works.

Shown below are the results of various strings passed to strtotime and converted to date strings.

For example:

echo date('l jS F (Y-m-d)', strtotime('-3 days'));
3 days ago
Wednesday 9th June (2021-06-09)
-3 days
Wednesday 9th June (2021-06-09)
-2 days
Thursday 10th June (2021-06-10)
yesterday
Friday 11th June (2021-06-11)
now
Saturday 12th June (2021-06-12)
today
Saturday 12th June (2021-06-12)
tomorrow
Sunday 13th June (2021-06-13)
+2 days
Monday 14th June (2021-06-14)
+3 days
Tuesday 15th June (2021-06-15)

As always there are often different ways to achieve the same result. In the list above you can see that '3 days ago' is equivalent to '-3 days'. Also 'yesterday' is the same as '-1 days' or '1 day ago'. The 's' on the end of 'days', 'years' or other measurements can be left off if you want.

The 'now' and 'today' input values in the list above are superfluous as if we leave off the second paramater of the date function it will default to the current time anyway.

2. Working with days of the week

Sometimes we work with days of the week and need to know the last/next occurrence of, for example, a Saturday:

-2 weeks Saturday
Saturday 29th May (2021-05-29)
last Saturday
Saturday 5th June (2021-06-05)
Saturday
Saturday 12th June (2021-06-12)
this Saturday
Saturday 12th June (2021-06-12)
first Saturday
Saturday 19th June (2021-06-19)
next Saturday
Saturday 19th June (2021-06-19)
third Saturday
Saturday 3rd July (2021-07-03)
+2 weeks Saturday
Saturday 26th June (2021-06-26)

In the list above we can see that 'Saturday', 'this Saturday' and 'first Saturday' are all equivalent. The 'this' and 'first' keywords are only used to improve readability.

Now it gets a bit complicated to explain. The following appears on the PHP website:

In PHP 5 up to 5.0.2, "now" and other relative times are wrongly computed from today's midnight. It differs from other versions where it is correctly computed from current time.

In addition, they've introduced 'second' as in 'second Saturday' and seem to have changed the meaning of 'next' to mean the same as 'first'. Finally, the return value on failure is now FALSE instead of -1. This last change is the most likely to cause problems in legacy applications.

The table below shows how the output has changed:

Older versions of PHP:PHP 5.0.2 and higher:
  • last Saturday
  • this Saturday or first Saturday
  • next Saturday
  • third Saturday
  • fourth Saturday
  • last Saturday
  • this Saturday or first Saturday or next Saturday
  • second Saturday
  • third Saturday
  • fourth Saturday

3. Using offsets to get the right day

Another problem is when you want to find the previous or next occurence of a certain day of the week. For example, today being a Saturday we can run the following:

last Saturday
Saturday 5th June (2021-06-05)
Saturday
Saturday 12th June (2021-06-12)
this Saturday
Saturday 12th June (2021-06-12)
first Saturday
Saturday 19th June (2021-06-19)
next Saturday
Saturday 19th June (2021-06-19)
third Saturday
Saturday 3rd July (2021-07-03)
Older versions of PHP:PHP 5.0.2 and higher:
  • last Saturday
  • this Saturday or first Saturday
  • next Saturday
  • third Saturday
  • fourth Saturday
  • last Saturday
  • this Saturday
  • first Saturday or next Saturday
  • second Saturday
  • third Saturday

You can see in the table above that, while 'next Saturday' returns the same result, the 'first', 'second', 'third', ... modifiers have all changed in meaning - thankfully to a more logical state.

You may notice also that this only occurs when you're calculating from the current day and not in the previous examples (unless today is Saturday). Whenever you do calculations based on days you should test as many possibilities as possible: Does it work today? yesterday? tomorrow? and so on.

Sometimes you want to find the last occurence of a day before today, or before yesterday or before 2 days ago. The following examples show the syntax you can use for this:

-3 days last Saturday
Wednesday 2nd June (2021-06-02)
-2 days last Saturday
Thursday 3rd June (2021-06-03)
yesterday last Saturday
Friday 4th June (2021-06-04)
tomorrow Saturday
Sunday 13th June (2021-06-13)
+2 days Saturday
Monday 14th June (2021-06-14)
+3 days Saturday
Tuesday 15th June (2021-06-15)

Now that you understand the inner workings of strtotime you should be able to drastically reduce the amount of code required to calculate past and future dates.

4. Checking if a date is in the Past or Future

The strtotime function returns an integer value so it's a simple matter to see if a date is in the past or in the future.

if(strtotime(dateString) > time()) { # date is in the future } if(strtotime(dateString) < time()) { # date is in the past } if(strtotime(dateString) == time()) { # date is right now }

Note: time() is equivalent to date('U') and returns the current timestamp to the nearest second.

You might want to replace time() with strtotime('0:00') if you want to compare a date string to the current date rather than the current date and time.



Source: https://www.the-art-of-web.com/php/strtotime/

Sunday, June 6, 2021

stripe cancel subscription using curl in php

stripe cancel subscription using curl in php

In this post we will show you stripe cancel subscription using curl in php, hear for stripe cancel subscription using curl in php we will give you demo and example for implement.

stripe cancel subscriptionCanceling subscriptions

Subscriptions can be canceled through the API:

1
2
3
   -u sk_test_a85RX6H48G5dfD55FDF77: \
   -X DELETE

Code for stripe cancel subscription using curl in php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
function curl_del($path)
{
    // Add your key
    $headers = array('Authorization: Bearer sk_test_a85RX6H48G5dfD55FDF77');
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_URL, $path);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $result = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
    curl_close($ch);
    return $result;
}
 
// add your subscriptions id
$curl_del = curl_del($path);
echo "<pre>";
print_r($curl_del);
echo "</pre>";

 cSource: https://www.onlinecode.org/stripe-cancel-subscription-using-curl-php/


How to backup and download Database using PHP

< ?php $mysqlUserName = 'databaseusername' ; $mysqlPassword = 'databasepassword' ; $mysqlHostNa...