$order_startdate = date('Y-m-d H:i:s'); // current date & time
$NewDate=Date('Y-m-d H:i:s', strtotime("+3 days", strtotime($order_startdate))); // 3 days increase Date And Time also
var_dump($NewDate);
I am get string(19) 2015-06-29 06:56:42
after refresh it has changed 2015-06-29 07:03:19
I want the time do not changed time i want
$NewDate I'm Used in NextPayment Date in Subscription Payment
For Now
i am in struggle !
Thanks
You are creating a new Date() instance every time the script loads, which means that every time you refresh the page new time value is returned because of the nature of time (it changes over time :D)
If you want to save the time, that means, you need to write the value of your variable when it is created into database/somewhere else.
Following from #Slim Kallari's answer, you could do this, to get a new date only when one is not already set:
if(!isset($_SESSION['NewDate']))
{
$NewDate=Date('Y-m-d H:i:s', strtotime("+3 days", strtotime($order_startdate)));
$_SESSION['NewDate'] = $NewDate;
}
else
{
$NewDate= $_SESSION['NewDate'];
}
var_dump($NewDate);
That way, you create a new Date instance if one doesn't already exists. Make sure to add session_start(); before creating your session variable, though.
Finally I research myself and implemented as working solution finally here it is best instead of dom just remove dom and try it should did a trick
$date = $order->order_date;// checkout placeorder date
$trial = '1';
$subsc = date('Y-m-d H:i:s', strtotime($date. '+ '. $trial . 'days'));
Related
I have used the following piece of code in some APIs to limit the request
sleep(1);
// date_value_from_db is taken from DB, question time as example
$date_value_from_db = '2022-12-31 11:18:00Z';
$lastCreatedAt = new DateTime($date_value_from_db);
$now = new \DateTime();
#echo $now->format('Y-m-d H:i:s');
$timeElapsedInSecs = $now->getTimestamp() - $lastCreatedAt->getTimestamp();
// Only execute if the time difference is more than 60 secs
if ($timeElapsedInSecs < 60) {
// throw Exception
}
// Proceed further
If you call this API in a loop, $now->format('Y-m-d H:i:s') returns the same value 3-4 times in a row, after that, it shows the correct current time. and so on. For example echo $now->format('Y-m-d H:i:s') in the above code looks like this:
2022-12-30 11:30:25
2022-12-30 11:30:25
2022-12-30 11:30:25
2022-12-30 11:31:32
2022-12-30 11:31:32
...
Also, the $timeElapsedInSecs value is also the same
Ideally, $now should give the current time for each call. But it's not happening.
Do you guys see any issues with the above code?
I created a file name testdt.php on my local system like this:
<?php
$now = new \DateTime();
echo $now->format('Y-m-d H:i:s')."\n";
When executing this script 100 time (I am on Windows) using:
for /L %f in (1,1,100) do #curl "http://localhost/test/testdt.php"
I do see the returned value change.
When you need more change, you should change 'Y-m-d H:i:s' to 'Y-m-d H:i:s.v', this will add milli-seconds
I have these two functions:
function time_is_older_than($timestamp, $time_string)
{
if (strtotime($timestamp) < strtotime('-' . $time_string))
return true;
return false;
}
function time_is_younger_than($timestamp, $time_string)
{
if (strtotime($timestamp) > strtotime('-' . $time_string))
return true;
return false;
}
They enable me to do neat things like:
if (time_is_older_than($last_time_some_action_happened, '5 minutes'))
do_it_again();
They normally work, except for during one hour every six months, when my timezone switches over to "summer time" or "winter time". This means that the clocks are increased or put back one hour at midnight (according to this timezone).
The PHP manual states this for strtotime:
The Unix timestamp that this function returns does not contain information about time zones. In order to do calculations with date/time information, you should use the more capable DateTimeImmutable.
However, if I provide the exact same date/time string, with "+08:00" added in the end versus "+00:00", for example, I get different numbers of seconds returned. So strtotime() does understand timezones when it parses the provided time, even if the returned integer obviously doesn't contain this information. (Nor is it expected or required to by me.)
I've spent countless hours trying to debug this, testing countless things, and just sitting here thinking, but I can't figure out what exactly would make the code I have fail, specifically for one hour. And especially what about it I need to change. Setting the second parameter for strtotime() seems likely, but I just couldn't make it work correctly.
My hottest "lead" for quite some time was that the strtotime('-' . $time_string) part is ending up using a different timezone than the timestamp strings provided, but I do provide timezone data to it most of the time! An example of $last_time_some_action_happened might be something like 2020-10-28 02:22:41.123456+01.
I set the timezone with date_default_timezone_set().
I suspect that I only need to make some very minor change, but I've been experimenting so much and so long now, even taking rests in between, that my brain can no longer see this clearly. I bet the solution is something awfully simple.
Please don't tell me to use DateTimeImmutable. This would fundamentally change my entire structure and require me to do things very differently. Perhaps I should, and even will, at some point, but for now, I just wish to fix this rare but still very annoying bug in my existing code. (If it's possible at all, which I very much believe is the case.)
I'm able to reproduce the issue you are having:
date_default_timezone_set('Pacific/Auckland');
// Daylight saving time 2020 in New Zealand began at 2:00am on Sunday, 27 September
$current = strtotime('2020-09-27 02:04:00');
$d1 = strtotime('2020-09-27 02:05:00', $current);
$d2 = strtotime('-5 minutes', $current);
var_dump($d1 > $d2); // false
var_dump(date('Y-m-d H:i:s', $d1)); // 2020-09-27 03:05:00
var_dump(date('Y-m-d H:i:s', $d2)); // 2020-09-27 03:59:00
This person looks to be having the same issue as you and may appear to be a bug.
DateTime::modify and DST switch
The solution is to convert the dates to UTC then compare:
// Convert to UTC and compare
$d1 = new \DateTime('2020-09-27 02:05:00', new \DateTimeZone('Pacific/Auckland'));
$d2 = new \DateTime('2020-09-27 02:04:00', new \DateTimeZone('Pacific/Auckland'));
$d2->setTimezone(new \DateTimeZone('UTC'));
$d2->modify('-5 minutes');
$d2->setTimezone(new \DateTimeZone('Pacific/Auckland'));
var_dump($d1 > $d2); // true
var_dump($d1->format(\DateTimeInterface::RFC3339_EXTENDED)); // 2020-09-27T03:05:00.000+13:00
var_dump($d2->format(\DateTimeInterface::RFC3339_EXTENDED)); // 2020-09-27T01:59:00.000+12:00
I've updated your functions:
function time_is_older_than($datetime, $time_string)
{
$d1 = new \DateTime($datetime);
$d1->setTimezone(new \DateTimeZone('UTC'));
$d2 = new \DateTime();
$d2->setTimezone(new \DateTimeZone('UTC'));
$d2->modify('-' . $time_string);
return $d1 < $d2;
}
function time_is_younger_than($datetime, $time_string)
{
$d1 = new \DateTime($datetime);
$d1->setTimezone(new \DateTimeZone('UTC'));
$d2 = new \DateTime();
$d2->setTimezone(new \DateTimeZone('UTC'));
$d2->modify('-' . $time_string);
return $d1 > $d2;
}
Could you consider a solution:
In the timestamp string(like Thu, 21 Dec 2000 16:01:07 +0200), add a timezone tag which specify timezone without difference of daylight saving time.
I have developed a subscription website. I am facing php date extend problem.
Please read my message-
Suppose today i have subscribe a package. Package duration 1 month. I mean 2014-05-08 to 2014-06-07
After two days i will again subscribe this same package. now the Package duration will extend 2014-06-07 to 2014-07-07
In my PHP code i am getting current date to +1 month but how to get after 1 month to next one month?
I have used this code:
date('Y-m-d h:i:s', mktime(0, 0, 0, date("m")+$getSubscription['Subscription']['duration'], date("d"), date("Y")));
You can use strtotime to achieve this.
Check this out:
<?php
// current subscription expiry date
$day = '2014-06-07';
// add 30 days to the date above
$new_date = date('Y-m-d', strtotime($day . " +30 days"));
// debug
echo $new_date;
Outputs: 2014-07-07
P.s. are you saving the newly calculated date into db or something and then using it on the next calculation?
Or use the DateTime object :
<?php
$date = new DateTime('2014-06-07');
$date_end = clone $date;
$date_end->add(new DateInterval('P1M');
echo $date_end->format('Y-m-d');
You'd save yourself a lot of hassle by using DateTime objects instead of messing around with date() and mktime(). In order to get today, simply:
$subscriptionDate = new DateTime(); // 2014-05-08
Then you can use the DateInterval class to add additional months, so if it's a monthly subscription, simply:
$subscriptionPeriod = new DateInterval('P1M');
$subscriptionDate->add($subscriptionPeriod);
Now, $subscriptionDate will be at 2014-06-08. You can keep adding as many months as you want and save it to your database, in the format you want, like:
$subscriptionDate->format('Y-m-d'); // 2014-06-08
$subscriptionDate->format('Y-m-d H:i:s'); // incl. time, e.g. 2014-06-08 12:02:45
See the manual for more examples.
I'm making a billing page.
In here, i have to get the latest 6 year-month based on the current month.
For example,
if i use,
date('Ym')
i get 201308
and if i use,
date('Ym')-1
i get 201307
ok, fine! But the problem is,
what if the current month is 201301?
in this situation if i do
date('Ym')-1
I get 201300, but i need 201212.
Any good idea?
Don't use date() for this. It wasn't made for it. Use DateTime() instead.
$dt = new DateTime();
$dt->modify('-1 month');
echo $dt->format('Ym');
See it in action
Or
$dt = new DateTime();
$dt->sub(new DateInterval('P1M'));
echo $dt->format('Ym');
See it in action
Is there a way to convert an input time string (ex: 01:13) to a Zend date object, so that I store it later in a timestamp column in a Mysql database.
Examples:
If the current datetime is 2013-07-15 17:33:07 and the user inputs 18:05 the output should be 2013-07-15 18:05:00.
If the current datetime is 2013-07-15 17:33:07 and the user inputs 02:09 the output should be 2013-07-16 02:09:00. Notice that since the time entered was lower than the current time, so it was treated as tomorrows time.
I simply want to get the next point in time that satisfies the entered time. I'm open for solution using plain PHP or Zend_Date.
I think you should compare the current time with the time entered by the user and create a DateTime object of either "today" or "tomorrow". DateTime accepts strtotime() relative time parameters.
Quick hack. Works as of today, 15.07.2013 23:58 local time:
$nextTime = new DateTime('today 18:10');
if ($nextTime < new DateTime('now')) { // DateTime comparison works since 5.2.2
$nextTime = new DateTime('tomorrow 18:10');
}
echo $nextTime->format('d.m.Y H:i:s');
here is working example for you just add your dynamic variable to check date with user inputs
You can use mktime function to manage your date.
$input_date = date("Y-m-d H:i:s",mktime(18,05,0,date("m"),date("d"),date("Y")));
echo "current time".$current_time = date('Y-m-d H:m:s');
echo "<br>User input is ".$input_date;
if(strtotime($current_time) > strtotime($input_date)){
$input_date = date("Y-m-d H:i:s",mktime(18,05,0,date("m"),date("d")+1,date("Y")));
echo "in";
}else{
// nothing to do
}
echo "<br> result->".$input_date;
i hope it will sure solve your issue