Adding to datetime in php - php

If i have:
if($date>=$start_interval...){
//Some code
}
Where both $date (current date) and $start_interval are both in datetime format - like YYYY-MM-DD 00:00:00. What do i need to do to add an hour (or 60 minutes) to $start_interval?
So the if statement would be - if($date>=$start_interval (+hour)) - in other words if its more than an hour later do something. How would you do this?
Many thanks in advance

Look into DateTime. It makes working with dates easy.
$now = new DateTime();
$start_interval = new DateTime('2012-12-14 00:00:00');
$start_interval->modify('+1 hour');
if ($now >= $start_interval)
{
// do something
}

<?php
if (strtotime($date) >= strtotime('+1 hour', strtotime($start_interval)) {
//Some code
}
More info: http://es1.php.net/manual/en/function.strtotime.php

Related

How to change date format to strtotime in codeigniter

I want to convert date format to strtotime in codeigniter to cross check the expire date from the database.
My code look like this below
$expiredate = date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s",strtotime($tra->t_started)). '+30days'));
if(strtotime(date("Y-m-d H:i:s")) > strtotime($expiredate)){
$ra->is_paid="1";
//package is expired for 30days.
} else{
$ra->is_paid="0";
//package is expired for 30days.
}
I need help to solve this problems. Thanks
Several issues here:
First, you don't need to convert dates with strtotime to compare them.
Second, to add a certain amount of days to a date, you use +30 day (singular) not +30 days
You could simply:
$now = date('Y-m-d H:i:s');
$expire_date = date('Y-m-d H:i:s', strtotime($tra->t_started."+30 day"));
if ($now >= $expire_date)
{
// 30 or more days overdue
}
else
{
// Less than 30 days overdue
}

Update a unix timestamp php (strtotime)

I want to update a unix timestamp and add x months.
This is a timestamp that i use 1456256866
strtotime("+1 month")
what i want to accieve is :
$time = '1456256866';
//update $time with x months something like
$time("+5 month");
Can someone put me in the right direction?
Much Thanks
You could do something like below. the function strtotime takes a second argument.
$time = 1456256866;
$time = strtotime('+5 month', $time);
For such operations You should use Datetime class, especially Add method:
$date = new DateTime('#1456256866');
$date->add(new DateInterval('P5M'));
echo $date->format('Y-m-d') . "\n";
Check more here: http://php.net/manual/pl/datetime.add.php

Setting Php and Mysql timestamp

I need to set timestamp eg. 4 hours ahead and 2 hours ahead separately
In my database, I have their columns as timestamp.
I know I could do something similar to this but am not sure if it's correct.
// For 4 hours ahead of time
$dt2 = date("Y-m-d 04:i:s");
//For 2 days ahead
$dt2 = date("Y-m-02 H:i:s");
//For 4 hours ahead of time
$dt2 = date("Y-m-d H:i:s", strtotime('+4 hours'));
//For 2 days ahead of time
$dt2 = date("Y-m-d H:i:s", strtotime('+2 days'));
In my mind it's much better to work with DateTime field and the DateTime class.
You have the ability so modify that objects very easily. For example:
$aktDate = new \DateTime();
Now you have the actual date and time in an object. If you want you can put a string insight the DateTime function so set your date manually.
$aktDate = new \DateTime('Y-m-d 04:i:s');
Not you can modify your dates if you want with the modify function.
in your case:
$pastDate = clone $aktDate;
$pastDate->modify('+2 days');
$futureDate = clone $aktDate;
$futureDate->modify('+4 days');
if($pastDate < $aktDate && $aktDate < $futureDate) {
// do something
}
I like the DateTime function much more because it's readable and you can work directly with your DateTime fields from your MySQL database if you have such fields. You can write that example much shorter but so you have better readability.
$date = new DateTime('now');
$date->modify('+2 days');
echo $date->format('Y-m-d H:i:s');
$date = new DateTime('now');
$date->modify('+4 hours');
echo $date->format('Y-m-d H:i:s');
You need to use the strtotime() function (http://php.net/manual/en/function.strtotime.php).
For your examples:
//+2 hours<br>
strtotime("+2 hours");
// +2 days<br>
strtotime("+2 days")
Edit: for what you ask, about posted values, the syntax is like this:
strtotime("+2".$_POST['field_name']." days");
You can use hours/days/months/weeks/years and either + or -

how to check if a date is three days before today

Hey i would like to know if there is any script (php) that could check if a specified date three days before today.
say..
$d1 = date("Y-m-d", filemtime($testfile));
$d2 = date("Y-m-d");
now i would like to know how to compare this two dates to check if d1 is atleast 3days ago or before d2
any help would be gladly appreciated.
Why not to use DateTime object.
$d1 = new DateTime(date('Y-m-d',filemtime($testfile));
$d2 = new DateTime(date('Y-m-d'));
$interval = $d1->diff($d2);
$diff = $interval->format('%a');
if($diff>3){
}
else {
}
Assuming you wish to test whether the file was modified more than three days ago:
if (filemtime($testfile) < strtotime('-3 days')) {
// file modification time is more than three days ago
}
Just check it with timestamp:
if (time() - filemtime($testfile) >= 3 * 86400) {
// ...
}
use date("Y-m-d", strtotime("-3 day")); for specific date
you can also use
strtotime(date("Y-m-d", strtotime("-3 day")));
to convert it to integer before comparing a date string
well, stunned to see no one is using mktime() function,
it makes the job simple
for example your input date is :10/10/2012
mktime convert it to unix time stamp
$check_date=mktime(0,0,0,10,**10+3**,2012);
we can perform any operations weather +,-,*,/
use timestamp instead of date,
$d1 = filemtime($testfile);
$now = time();
if ($now - $d1 > 3600*24*3) {
..
}

how to find number of days by subtracting previous date from current date

Is it possible to find the no of days between two date fields.
i want to remove the user if user does not login within 30 days.
on every login login_date field will update.
i want to subtract two fields login_date and current_date
and if answer is 30 or greater than 30 it will delete that user.
i am new in php and need help..
i am working on localhost.
I suggest to use DateTime and DateInterval objects.
$date1 = new DateTime("2007-03-24");
$date2 = new DateTime("2009-06-26");
$interval = $date1->diff($date2);
echo "days difference ".$interval->d." days ";
read more php DateTime::diff manual
If you use timestamp format - you can use condition:
$month = 30*86400;
if ($current_date - $login_date > $month){
delete_user();
}
If you use datetime format - you can transform this format to timestamp with function strtotime
Hi try this function will get number of days between two dates.
function dateDiff($start, $end) {
$start_ts = strtotime($start);
$end_ts = strtotime($end);
$difference = $end_ts - $start_ts;
return round($difference / 86400);
}
thanks
Following will return exact days between two dates.
$last_login_date = "2012-04-01";
$current_date = "2012-04-30";
echo "Days: ".round(abs(strtotime($current_date)-strtotime($last_login_date))/86400);
Hope this helps.

Categories