I'm trying to do a function that will check the php date against what is in the database. If the current date (YYYY-MM-DD HH:MM:SS) is 24 hours after the stored database date, then it will continue, otherwise it won't do anything.. If that makes sense?
Here's how I'm trying to get it to work, but not 100% sure how:
$now = date("Y-m-d H:i:s");
foreach($results as $item) {
if ( /*item date is 24 hours or more earlier than $now*/ ) { /* this is what I'm not sure how to do */
//do something
} else {
//do nothing
};
};
If someone could help me get this working, I'd greatly appreciate it. I just don't know how to compare $item->date with $now and see if $item->date is 24 hours or more behind $now..
You can use strtotime instead of date math. Personally I think it reads much better.
if (strtotime($item->date) <= strtotime('-1 day')) {
// date is more than or equal to 24 hours old
}
foreach($results as $item) {
if (time() >= strtotime($item['date']) + 86400) {
// current time is 86400 (seconds in one day) or more greater than stored time
} else {
//do nothing
};
};
Note that if the stored date isn't in the same timezone as the server you'll need to mess with timezones - see here for starters.
Note that I store dates and times in MySQL as Unix timestamps to avoid some of these problems. I know it's not good database practice, but then PHP stores times as Unix timestamps anyway.
Related
I am wondering if there is an easy way to do this, i'm pulling a timestamp from mysql, i then want to check if the timestamp is less than 24 hours ago, if it is, i do nothing, else i will do an action, my code:
$dbStoredDate = $theDate['site_date'];
$theTimeMinus = strtotime('-1 day', $dbStoredDate);
if ($theTimeMinus <= $dbStoredDate) {
}
This is what i have come up with, i realize now it would have been better if i used time() instead of timestamp() in mysql, lesson learned, the first value is coming from mysql, the second is just deducting 1 day, does the logic look ok do you think? thanks for any input guys.
You can do this easily (assuming you're using unix timestamp, if not you can convert it):
$continue = ((time() >= ($theDate['site_date']+86400));
if ($continue)
echo "older than/equal to 24 hours";
else
echo "not older than 24 hours";
How exactly is this done? There's so many questions on stack-overflow about what I'm trying to do; However all of the solutions are to edit the MYSQL Query, and I need to do this from within PHP.
I read about the strtotime('-30 days') method on another question and tried it, but I can't get any results. Here's what I'm trying:
$current_date = date_create();
$current_date->format('U');
... mysql code ...
$transaction_date = date_create($affiliate['Date']);
$transaction_date->format('U');
if($transaction_date > ($current_date - strtotime('-30 days'))) {
} else if(($transaction_date < (($current_date) - (strtotime('-30 days'))))
&& ($transaction_date > (($current_date) - (strtotime('-60 days'))))) {
}
Effectively, I'm trying to sort all of the data in the database based on a date, and if the database entry was posted within the last 30 days, I want to perform a function, then I want to see if the database entry is older than 30 days, but not older than 60 days, and perform more actions.
This epoch math is really weird, you'd think that getting the epoch of the current time, the epoch of the data entry, and the epoch of 30 and 60 days ago would be good enough to do what I wanted, but for some reason it's not working, everything is returning as being less than 30 days old, even if I set the date in the database to last year.
No need to convert to unix timestamp, you can already compare DateTime objects:
$current_date = data_create();
$before_30_day_date = date_create('-30 day');
$before_60_day_date = date_create('-60 day');
$transaction_date = date_create($affiliate['Date']);
if ($transaction_date > $before_30_day_date) {
# transation date is between -30 day and future
} elseif ($transaction_date < $before_30_day_date && $transaction_date > $before_60_day_date) {
# transation date is between -60 day and -30 day
}
This creates (inefficiently, see my comment above) an object:
$current_date = date_create(date("Y-m-d H:i:s"));
From which you try to subtract an integer:
if($transaction_date > ($current_date - strtotime('-30 days'))) {
which is basically
if (object > (object - integer))
which makes no sense.
you're mixing the oldschool time() system, which deals purely with unix timestamps, and the newer DateTime object system, which deals with objects.
What you should have is
$current_date = date_create(); // "now"
$d30 = new DateInterval('P30D'); // 30 days interval
$transaction_date = date_create($affiliate['Date']);
if ($transaction_date > ($current_date->sub($d30)) { ... }
You might consider DatePeriod class, which in essence gives you the ability to deal with a seires of DateTime objects at specified intervals.
$current_date = new DateTime();
$negative_thirty_days = new DateInterval::createFromDateString('-30 day');
$date_periods = new DatePeriod($current_date, $negative_thrity_days, 3);
$thirty_days_ago = $date_periods[1];
$sixty_day_ago = $date_periods[2];
Here you would use $thirty_days_ago, $sixty_days_ago, etc. for your comparisons.
Just showing this as alternative to other options (which will work) as this is more scalable if you need to work with a larger number of interval periods.
I need to create a PHP script that pulls the timestamps of various stuff from a database (logs, messages, logins, etc) and removes them if they are older than X amount of days. I am poor at doing work with time and am a bit stumped on the best way to do this.
I realize I could separate the day/month/year in the string using explode() and compare these with a bunch of If statements, but would like to use a more efficient method. Something like the following would be the correct way to do this correct?
$dt = "2011-03-19 10:05:44";
//if $dt is older than 90 days
if((time()-(60*24*90)) > strtotime($dt))
{
}
Subtract (minutes*hours*days) from time() or are the numbers wrong?
You can use DateTime class for this. Example:
$dt = "2011-03-19 10:05:44";
$date = new DateTime($dt);
$now = new DateTime();
$diff = $now->diff($date);
if($diff->days > 90) {
echo 'its greater than 90 days';
}
I want to check if 30 min passed after created time in database. created is a time column having time stamp in this format 1374766406
I have tried to check with date('m-d-y H:i, $created) but than of course it is giving human readable output so don't know how to perform check if current time is not reached to 30min of created time.
Something like if(created > 30){}
Try this:
$created = // get value of column by mysql and save it here.
if ($created >= strtotime("-30 minutes")) {
// its over 30 minutes old
}
The better approach is to use DateTime for (PHP 5 >= 5.3.0)
$datenow = new DateTime();
$datenow->getTimestamp();
$datedb = new DateTime();
$datedb->setTimestamp(1374766406);
$interval = $datenow->diff($datedb);
$minutes = $interval->format('%i');
$minutes will give you the difference in minutes, check here for more
http://in3.php.net/manual/en/datetime.diff.php
Here is the working code
http://phpfiddle.org/main/code/jxv-eyg
You need to use strtotime(); to convert the date in human form back to a timestamp, then you can compare.
EDIT: Maybe I misread.
So something like;
if(($epoch_from_db - time()) >= 1800){
//do something
}
I have a week calendar that holds events, and want that users can't add events for the past days. So I'm tring to use a function like that:
if( strtotime($this->day) < time() ){ // date format is YYYY-MM-DD
// date is past
}else{
// date is not past
}
It seems to works fine, except that it consider today date as a past day. What am i doing wrong?
Simpler ->
if(strtotime($this->day) < strtotime(date('Y-m-d')))
{
...
}
else
{
...
}
A timestamp never contains only the date, but is always down to the current second. strtotime($this->day) is going to return today's date at 0:00, while you are comparing it against now, say, 11:12.
You could use strtotime("$this->day 12:59:59pm"); (if the format of $this->day allows for that) or use tomorrow's timestamp.
if(strtotime($this->day) < mktime(0, 0, 0)){
// date is past
} else {
// date is not past
}