PHP - Resetting Stats - php

I'm using a WordPress plugin that re-sets its stats every 7 days using the following line of code:
$keep_time = 60*60*24*7; // 7 days for now (TODO: admin setting)
Could someone help me to modify the code to re-set the stats every 6 hours or every other day?
I did try to try to change the 7 to 1 but it doesn't work. Probably the solution is very simple, but unfortunately I'm not a PHP programmer.
Thanks everyone for answering my question, wanted to give a vote but I don't have enough 'reputation'

For 6 hours use:
$keep_time = 60*60*6;
For 2 days use:
$keep_time = 60*60*24*2;
The value is in seconds. 60*60 is the number of seconds in an hour. Then you multiply by the number of hours you want. If you want multiple days, you multiply by 24 hours in a day, and then the number of days.

I like DateTime() and DateInterval() for this. Not only is it clearer but it handles daylight savings time and leap years as well as those pesky last days of the month.
7 Days:
$start_time = new DateTime(); // "now" as an example
$keep_time = new DateInterval('P7D'); // 7 days
$start_time->add($keep_time);
echo $start_time->format('Y-m-d');
6 Hours
$start_time = new DateTime(); // "now" as an example
$keep_time = new DateInterval('PT6H'); // 6 hours
$start_time->add($keep_time);
echo $start_time->format('Y-m-d');

$keep_time = 60(sec)*60(min)*24(hours)*7(days);
you need to do
$keep_time = 60*60*6;

Related

PHP DateInterval - Where is string $format parameter defined?

Been trying to figure out just how to add hours, days, weeks and years to a date. Found a few examples that work, but I have NO idea why.
$dt->add(new DateInterval('P1Y')); 'P1M', 'P1D' all add one year, month and day. 'P1H' or 'P1S' throw exceptions.
Been reading all about DateTime class and reading the https://www.php.net/manual/en/dateinterval.format.php page, NO WHERE can I find anything that explains what the 'P' part of that format string is.
Where is some decent documentation on this??? It should not take hours to figure out how to add a few days to a date!!
The P stands for period. If you want to define an interval based on hours or minutes check this example:
$interval = new DateInterval('PT1H');
Here, $interval represents a time interval of 1 hour.
For example, to add an hour to an existing date:
$date = new DateTime();
$date->add($interval);

Wrong time with DateTime

I am creating notifications system and here is a task: get the time, when a notification was sent. I mean the following: 1 minute ago, 13 hours ago and so on. I have already made up a script but it shows wrong time. For example instead of showing '5 minutes ago' it shows '9 hours ago'. Here is the alrogithm:
Get old timestamp from database. Old timestamp is the time, when a
notification was sent.
Get current users timestamp.
Get difference between them.
Echo result.
Here is the PHP code:
$fromdb = '1503737539'; //For this example think, that this variable is from database.
//This timestamp was created 5 minutes earlier, so in result it should show '5 minutes ago'.
$curr = new DateTime();
$got2 = new DateTime(date('Y-m-d H:i:s',$fromdb));
$interval = $curr->diff($got2);
echo $interval->format('%d')." days ".$interval->format('%h')." Hours ".$interval->format('%i')." Minutes ".$interval->format('%s')." Seconds";
The output is:
0 days 9 hours ....
instead of
0 days 0 hours 5 minutes ....
How can I fix that? I guess that this is a problem with timezones. But how can I guess guests timezone though?
Update:
Change code:
$got2 = new DateTime(date('Y-m-d H:i:s',$fromdb));
And interesting fact: The more is actual difference, the less time it shows in output. For example: Old timestamp was created at 8:00 am, and current is 15:00 pm, it shows 0 days 1 hour in output.
I think
date('Y-m-d',$fromdb)
broke your code.
Try
$got2->setTimestamp($fromdb)
there is mistake in your code. you format date without hours, minutes and seconds
$got2 = new DateTime(date('Y-m-d',$fromdb));
try
$got2 = new DateTime(date('Y-m-d H:i:s',$fromdb));
Try
$date = new DateTime(); echo $date->format('U = Y-m-d H:i:s');
$date->setTimestamp(1171502725);
echo $date->format('U = Y-m-d H:i:s');

PHP Show next 7 Days range

I saw this response to a thread and it works but doesn't create a range between the current day and seven days time. Which is what I need. Can anyone give me a hand please?
Update:
An easier way to word it. I want to Select data between two dates. For example current day and seven days time.
My current code:
if($current_day) {
$data['current_day']=date('Y-m-d', strtotime('+7 days'));
$now = new DateTime();
$future_date = new DateTime('2011-05-11 12:00:00');
$interval = $future_date->diff($now);
echo $interval->format("%a days, %h hours, %i minutes, %s seconds");
You can use this code sample if you have php 5.3 or above,
Else try to calculate the difference of two dates in seconds using time() and strtotime(). Then translate those seconds into days/hours/minutes/seconds.

Calculate Time difference using UNIX time stamp in PHP [duplicate]

This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 9 years ago.
I have 2 unix time, one is todays date and other is expiration time.
Todays Time: 1377173245 (2013-08-22 12:07:25)
Expiration Time: 1406303166 (2014-07-25 15:46:06)
What I want to achieve is calculate remaining time to expire the listing. In current case, it should show, 11 months -- hours -- minutes -- seconds I didn't find any good solution to calculate the difference. I doubt is it possible to calculate time difference using UNIX time system?
Thank you :-)
This is what you're looking for: DateTime::diff
First create object with 1st date:
$date = new DateTime('2013-08-22 12:07:25');
Then use the diff method:
$diff = $date->diff(new DateTime('2014-07-25 15:46:06'));
Now if you print $diff variable, you will see the array containing values for hour, minute, day and so on.
Use DateTime::diff
See the doc on https://www.php.net/manual/en/datetime.diff.php
Try using datetime::diff. Here's the example from the php.net documentation:
<?php
$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%a days');
?>
It's easy to create a DateTime object using a timespamp. However, this solution requires PHP 5.3+.
Use DateTime class:
Example:
$datetime1 = new DateTime();
$datetime2 = new DateTime('2014-07-25 15:46:06');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%M months, %H hours, %I minutes, and %S seconds remaining ');
Outputs:
11 months, 21 hours, 44 minutes, and 20 seconds remaining
It is most definitely possible. However, you'll run into problems with months, because there are different number of days in each month, so I'll just do days, hours, minutes, and seconds below.
$days=($expiration-$today)/(60*60*24);
$hours=(($expiration-$today)/(60*60))%24; //the modulus operator, gets a remainder
$minutes=(($expiration-$today)/(60))%(60*60);
$seconds=($expiration-$today)/60;
If you need months, you'll find it easier to use date_diff().

days to hours using date() function

I have time in minutes and I want to find out how many hours it is. But with attached code, for 1600 minutes, I get 2 hours and 40 minutes. I need it in format 26:40:00. Thanks for help.
$my_time = 1600;
echo date("H:i:s", $my_time);
Try using a DateInterval object, which is specifically built to handle intervals of time:
$interval = new DateInterval('M1600');
echo $interval->format('%H:%i:%s');
date isn't really suited for this. Instead, try this:
echo sprintf("%s:%2s:%2s",floor($my_time/3600),floor($my_time/60)%60,$my_time%60);
(This is assuming you have $my_time in seconds, not minutes. Multiply by 60 up front to get the time in seconds)

Categories