Find Dates Before Specified Date and Insert Them Using PHP & SQL - php

I have a date outputted from the database:
$deadline = $row['DEADLINE'];
When I print $deadline it returns: 2015-05-03 18:00:00
Now I want a way to find each day 24 hours before up until todays date and then use each of those values to insert in a new table in the database.
So in this case I want:
2015-05-03 18:00:00
2015-05-02 18:00:00
2015-05-01 18:00:00
2015-04-30 18:00:00
2015-04-29 18:00:00
To be inserted into the database 5 new rows. So I know I need todays date:
$now = date("Y-m-d H:i:s");
Then I have the date of the deadline:
$deadline = $row['DEADLINE'];
I can then find the difference in days:
$dateDiff = ($deadline - $now)/(24*60*60);
This gives me a figure (in this case it will be 5). Then could I use this figure to display the date and time for 5 days prior?
Not sure how I would insert them all though. Your help would be appreciated.

Use DateTime object, really easiest way :
PHP :
$now = new datetime();
$sub = new datetime();
for($i=0;$i<5;$i++)
{
$sub->sub(new DateInterval('P1D'));
echo $sub->format('Y-m-d H:i:s').'<br />';
echo $sub->diff($now)->format('%a days').'<br />';
}
OUTPUT :
2015-04-29 14:18:09
1 days
2015-04-28 14:18:09
2 days
2015-04-27 14:18:09
3 days
2015-04-26 14:18:09
4 days
2015-04-25 14:18:09
5 days

This isent perfect, but it will probably get you started
Try Using Datetime http://php.net/manual/en/class.datetime.php
Or Carbon/Carbon Package: https://github.com/briannesbitt/Carbon
$datetime = new DateTime("2015-04-03 18:00:00");
$now = new DateTime('NOW');
$difference = $datetime->diff($now);
echo '<pre>';
for($i = 1; $i <= $difference->days; $i++)
{
if($difference->invert){
var_dump (
$temp = $datetime
->modify('-1 day')
->format('Y-m-d H:i:s')
);
}else{
var_dump (
$temp = $datetime
->modify('+1 day')
->format('Y-m-d H:i:s')
);
}
}

Related

How to generate dynamic expiry dates like 3 months, 6 months, 9 months and 12 months from the today date in php

I want to create dynamic expiry date from the today to next 3 months or 6 months or 9 months or 12 months using PHP
I tried but it's not working
$today_date= strtotime(date("Y-m-d"));
$data['expiry_date'] = strtotime(date("Y-m-d", strtotime($valid_months,date("Y-m-d"))));
display code like this
<?php foreach($mydata as value){
$date_expire=date('d M Y',strtotime($value->expiry_date));
$register_date=date_create(date('d M Y ',$value->created));
$expiry_date=date_create($date_expire);
$diff = date_diff($register_date,$expiry_date);
echo $diff->format('%a Days');
}
I am getting output like this
2348824 Days // count remaining days
You could use the DateTime class and the associated methods available such as add and diff
$interval=new DateInterval('P3M');
$now = new DateTime();
$start=new DateTime();
$end=new DateTime( date( DATE_ATOM, strtotime('+1 year') ) );
$end->add( $interval );
while( $start->add( $interval ) <= $end ){
echo $diff = $start->diff( $now )->format('%a') . '<br>';
}
This will output:
92
182
274
366
As far as i understand from your question,i think you just need this.
echo date('d/m/Y', strtotime('+3 months'));
If it Doesn't help , feel free to comment .

reduce month in timestamp php

I need a list which iterates through the months and always shows the last days like this
2015-04-30
2015-03-31
2015-02-28
...
My idea was to do this with the strtotime method, where '1430344800' is the timestep according to 2015-04-30
$time_temp = 1430344800;
echo date('Y-m-t',$time_temp)."<br>";
$time_temp = strtotime("-1 month",$time_temp);
echo date('Y-m-t',$time_temp)."<br>";
$time_temp = strtotime("-1 month",$time_temp);
echo date('Y-m-t',$time_temp)."<br>";
but I just get
2015-04-30
2015-03-31
2015-03-31
replacing 'Y-m-t' by 'Y-m-d' gives
2015-04-30
2015-03-30
2015-03-02
Why is it not reducing the month properly and how can I accomplish it?
$lastDayOfMonth = time(); // depending on what you're trying to do, this could change.
// For example, it could be = strtotime("+1 month");
for( $i = 0; $i < $numberOfMonthsToShow; $i++ ){
$lastDayOfMonth = strtotime("last day of previous month", $lastDayOfMonth);
echo date('Y-n-j', $lastDayOfMonth).'<br />';
}

php DateTime diff method behavior

I have to find the days until an expiration date.
I tried to use diff method of DateTime class.
$dataexp = 2013-11-06 00:00:00 ;
$now = 2013-11-05 13:00:00 ;
$dtn = new DateTime('now');
$dte = new DateTime($dataexp);
$diff = $dtn->diff($dte);
$days = sprintf("%01d", $diff->days);
$days ---> display 1
My problem is if the dataexp is in the past of 1 day the result of diff is 1 and not -1
$dataexp = 2013-11-04 00:00:00 ;
$now = 2013-11-05 13:00:00 ;
$dtn = new DateTime('now');
$dte = new DateTime($dataexp);
$days = sprintf("%01d", $diff->days);
$days ---> display 1
What method could I use to get what I want? (-1 days)? Thanks
See DateInterval::format(), specifically the r format character.
echo $diff->format('%r%d');

For loop involving a range of dates

I have a date range in which I want to process between each day. So for example between
2013-03-01 00:00:00 and 2013-04-01 00:00:00 there are 31 days
so my for loop is something like this
$date_next = $date_from;
for($i=0;$i<31-1;$i++)
{
$date_next_str = new DateTime($date_next);
$date_next_1_str = new DateTIme($date_next);
$date_next_1_str->modify("+1 day");
$date_next_1_str->modify("-1 second");
$date_next_1_str->modify("+1 day");
$date_next = $date_next_1_str->date;
}
so in my first loop it will be from 2013-03-01 00:00:00 to 2013-03-01 23:59:59
However when I assign $date_next_1_str->date to $date_next at the end of the for loop, the $date_next still shows 2013-03-01 23:59:59, which was supposed to be 2013-03-02 00:00:00.
Anyone can help me with this? Thanks in advance!
You can do this quite easily using PHP's DateTime, DateInterval and DatePeriod objects:-
$startDate = \DateTime::createFromFormat('Y-m-d H:i:s', '2013-03-01 00:00:00');
$endDate = \DateTime::createFromFormat('Y-m-d H:i:s', '2013-04-01 00:00:00');
$interval = new \DateInterval('P1D');
$endDate->add($interval); //As otherwise last day will be missed off.
$period = new \DatePeriod($startDate, $interval, $endDate);
foreach($period as $date){
//each $date is an instance of DateTime
var_dump($date); // Or whatever you want to do with the DateTime object
}

php - find date for same day of week for last year

So, in PHP i'm trying to return a date value for last year based on the same day of week.
EX: (Monday) 2011-12-19 inputted should return (Monday) 2010-12-20.
I was just doing it simply by -364 but then that was failing on leap years. I came across another function :
$newDate = $_POST['date'];
$newDate = strtotime($newDate);
$oldDate = strtotime('-1 year',$newDate);
$newDayOfWeek = date('w',$oldDate);
$oldDayOfWeek = date('w',$newDate);
$dayDiff = $oldDayOfWeek-$newDayOfWeek;
$oldDate = strtotime("$dayDiff days",$oldDate);
echo 'LAST YEAR DAY OF WEEK DATE = ' . date('Ymd', $oldDate);
however, that is failing when you try to input a Sunday date, as it does a 0 (sunday) minus 6 (saturday of last year date), and returns with a value T-6. IE inputting 2011-12-25 gets you 2010-12-19 instead of 2011-12-26.
I'm kind of stumped to find a good solution in php that will work for leap years and obviously all days of the week.
Any suggestions?
Thanks!
How about this, using PHP's DateTime functionality:
$date = new DateTime('2011-12-25'); // make a new DateTime instance with the starting date
$day = $date->format('l'); // get the name of the day we want
$date->sub(new DateInterval('P1Y')); // go back a year
$date->modify('next ' . $day); // from this point, go to the next $day
echo $date->format('Ymd'), "\n"; // ouput the date
$newDate = '2011-12-19';
date_default_timezone_set('UTC');
$newDate = strtotime($newDate);
$oldDate = strtotime('last year', $newDate);
$oldDate = strtotime(date('l', $newDate), $oldDate);
$dateFormat = 'Y-m-d l w W';
echo "This date: ", date($dateFormat, $newDate), "\n";
echo "Old date : ", date($dateFormat, $oldDate);
That gives:
This date: 2011-12-19 Monday 1 51
Old date : 2010-12-20 Monday 1 51
Use strtotime() to get a date, for the same week last year.
Use the format {$year}-W{$week}-{$weekday}, like this:
echo date("Y-m-d", strtotime("2010-W12-1"));
And you can do that for as long back you wan't:
<?php
for($i = 2011; $i > 2000; $i--)
echo date("Y-m-d", strtotime($i."-W12-1"));
?>
Make it easier :)
echo date('Y-m-d (l, W)').<br/>;
echo date('Y-m-d (l, W)', strtotime("-52 week"));
Edit: I forgot to write output: :)
2015-05-06 (Wednesday, 19)
2014-05-07 (Wednesday, 19)
<?php
$date = "2020-01-11";
$newdate = date("Y-m-d",strtotime ( '-1 year' , strtotime ( $date ) )) ;
echo $newdate;
?>
ref https://www.nicesnippets.com/blog/how-to-get-previous-year-from-date-in-php

Categories