Issues with Adding 1 month to a Unix time stamp - php

For some reason, I cannot get strtotime('+1 month) to work. Here is my code;
$Date = $_REQUEST['date']; //This is a unix time stamp
$Start = $_REQUEST['start']; //This is a unix time stamp
$End = $_REQUEST['end']; //This is a unix time stamp
to add a month onto my dates;
$monStart =strtotime('+1 month', $Start);
$monEnd =strtotime('+1 month', $End);
$monDate =strtotime('+1 month', $Date);
then to show my changed dates;
$vEnd = date('m/d/Y', $monEnd);
$vStart = date('m/d/Y', $monStart);
$vDate = date('m/d/Y', $monDate);
The problem that I have is that the supplied dates;
$Date = 1/31/2013
$Start = 1/01/2013
$End = 1/31/2013
Return;
$vDate = 3/03/2013
$vStart = 2/01/2013 //Only correct one
$vEnd = 3/03/2013
Please can someone help me?

It's jumping to March because today is 31sth Jan, and adding a month gives 31st Feb, which doesn't exist, so it's moving to the next valid date. This is a PHP bug. You can get more info on that at https://bugs.php.net/bug.php?id=44073
You can try with DateTime to over come this scenario. You can use this function for your requirement
function add_month($date_value, $months, $format = 'm/d/Y') {
$date = new DateTime($date_value);
$start_day = $date->format('j');
$date->modify("+{$months} month");
$end_day = $date->format('j');
if ($start_day != $end_day)
$date->modify('last day of last month');
return $date->format($format);
}
Now you can call :
$vEnd = add_month($monEnd, 1);
$vStart = add_month($monStart, 1);
$vDate = add_month($monDate, 1);
This will give you :
$vDate = '02/28/2013';
$vStart = '02/01/2013';
$vEnd = '02/28/2013';
Hope this helps you :)

DateTime is much better for handling date math as it account for things like days in the month:
$dt = new DateTime('2013-02-01');
$dt->modify('+1 month');
echo $dt->format('Y-m-d');
See it in action
Since you're using timestamps it might look like this:
$dt = new DateTime('#'.$_REQUEST['start']);
$dt->modify('+1 month');
echo $dt->format('m/d/Y');

Related

Adding a month to Date returns wrong day

I want to add a month to current Date. I found a code here on stackoverflow and it works - but, it returns a wrong day. It removes a day from the current date.
Code:
function add_months($months, DateTime $dateObject)
{
$next = new DateTime($dateObject->format('Y-m-d H:i:s'));
$next->modify('last day of +'.$months.' month');
if($dateObject->format('d') > $next->format('d')) {
return $dateObject->diff($next);
} else {
return new DateInterval('P'.$months.'M');
}
}
function endCycle($d1, $months)
{
$date = new DateTime($d1);
$newDate = $date->add(add_months($months, $date));
$newDate->sub(new DateInterval('P1D'));
$dateReturned = $newDate->format('Y-m-d H:i:s');
return $dateReturned;
}
$today = date("Y-m-d H:i:s");
$ymd = DateTime::createFromFormat('Y-m-d H:i:s', $today)->format('Y-m-d H:i:s');
echo(endCycle($ymd, 1)); //returns wrong day
So when I buy a month, I get 29 days. When I buy again, I get -2 days, because it removes the day from the existing expire date in the database.
What´s wrong with the date calculation?..
Thank you :)
Best regards
Please, don't overcomplicate a simple task.
http://php.net/manual/en/datetime.modify.php
<?php
$date = new DateTime('2006-12-12');
$date->modify('+1 month');
echo $date->format('Y-m-d');
?>
That's all you need.

Find date of after 2 months using php

When i have current date is in "31/12/2017". I need to find date of after 2 months that means its February. When its february i need to get as "29/2/2018". But When we use below code i got "03/03/2018". Can you please help me to solve this task,
Here i added my PHP code,
$xmasDay = new DateTime('2017-12-31 + 2 month');
echo $xmasDay->format('Y-m-d');
please try this
<?php
function add_month($date_str, $months)
{
$date = new DateTime($date_str);
// We extract the day of the month as $start_day
$start_day = $date->format('j');
// We add 1 month to the given date
$date->modify("+{$months} month");
// We extract the day of the month again so we can compare
$end_day = $date->format('j');
if ($start_day != $end_day)
{
// The day of the month isn't the same anymore, so we correct the date
$date->modify('last day of last month');
}
return $date->format('Y-m-d');
}
$result = add_month('2017-12-31', 2);
echo $result
calculate from first of the month, then use date t
$orginal = '2017-12-31';
$orginal = explode('-', $orginal);
$originalDate = strtotime($orginal[0] . '-' . $orginal[1] . '-01 00:00:00');
$newDate = strtotime('+2 month', $originalDate);
echo date('Y-m-t', $newDate);
// or
$date = new DateTime();
$date->setTimestamp($newDate);
echo $date->format('Y-m-t');

get next immediate same date using php

i need to get immediate date like if i select date is 05 then output will be 2017-07-05 cause select date already passed
if i select 12 then output will be 2017-06-12 cause this date future date
final if i select previous date of current month then output will be next month same date and if i select future date of current month then output will be same month
i have tired but not working this
$today = date("Y-m-d");
$next_payment_date = date('Y-m-d', strtotime('+1 month', $today));
or
$time = time();
date("Y-m-d", mktime(0,0,0,date("n", $time),date("j",$time)- 1 ,date("Y", $time)));
thanks in your advance
One more option:
https://3v4l.org/Me2Kh
$input = 12;
$day = date("d");
if ($input > $day){
$date = date("Y-m-"). str_pad($input,2,"0", STR_PAD_LEFT);
}else{
$date = date("Y-m-",strtotime("+1 month")). str_pad($input,2,"0", STR_PAD_LEFT);
}
echo $date;
I use str_pad to keep two digit day number.
Try this -
<?php
$day = '05';
$today = date('Y-m-d');
$supplied = date('Y-m-'.$day);
if($today>$supplied){
$final = date('Y-m-d', strtotime("+1 months", strtotime($supplied)));
}
else{
$final = $supplied;
}
echo $today;
echo '<br />';
echo $supplied;
echo '<br />';
echo $final;
What I'm doing here -
Comparing the current and supplied date
Based on comparison, if supplied date is smaller, I'm adding 1 month else dislpaying the supplied date.
use this,
$today = date('Y-m-d');
$nextDate = date('Y-m-d', strtotime('+1 month'));
or $nextDate = date('Y-m-d', strtotime('+1 month', strtotime($today));
I think this may help.
I would consider using DateTime and it's add method for a DateInterval.
$date = new \DateTime('now', new \DateTimeZone('America/New_York'));
$interval = new \DateInterval('P1M');
$date->add($interval);
Here are the supported DateTimeZone values. Make sure to set that to the applicable time zone.
Edit:
DateTime is mutable, so please keep that in mind.
Try this code :
<?php
$selected_date = '2017-06-05';
$current = date('Y-m-d');
//echo $current;
if($selected_date < $current)
{
$newDate = date('Y-m-d',strtotime($selected_date."+1 month"));
echo $newDate; // gives 2017-07-05
}else if($selected_date > $current)
{
$newDate = $current;
echo $newDate; // gives 2017-06-07
}
?>
From what you described in the points at beginning of the question, you could achieve it this way:
$selectedDate = new DateTime('2016-06-05 00:00:00');
$now = new DateTime('now');
$now->setTime(0, 0, 0);
if ($selectedDate < $now) { // Selected date is in past
// Set month and year to current
$selectedDate->setDate(
date('Y'),
date('m'),
$selectedDate->format('d')
);
// Add 1 month
$selectedDate->add(new DateInterval('P1M'));
}
// If selected date is current or in future we do nothing
echo $selectedDate->format('Y-m-d');
For input 2017-06-05 it will return 2017-07-05 as expected, and for current or future date will return the date that was selected. Works also for any past date like 2016-04-05

How to verify if specific date is monday in php

i have a script that generates week days with a next and preview. It workes perfect but i also added a callendar that is autosubmiting on change. the problem i have with it is that when i select a day and that day is monday is still goes me to last week. What i need is to verify if the selected day is monday and give $monday that date instead giving it the last week monday. Here is my script:
$date = strtotime(date($_GET['date']));
$monday = "";
if (isset($_GET['n_startdate'])) {
$date = $_GET['n_startdate'];
$lastweek = strtotime("next week", $date);
$monday = strtotime("last Monday", $lastweek);
} else if ( isset($_GET['p_startdate'])) {
$date = $_GET['p_startdate'];
$lastweek = strtotime("-7 days", $date);
$monday = strtotime("last Monday", $date);
} else if($date!='' ){
$monday = strtotime("last Monday", $date);
} else {
$date = strtotime(date('Y-m-d H:i:s'));
$monday = strtotime("last Monday", $date);
}
edit: i don't get why i got -1 to my question :(
If you want to find out if a given date is a day of the week, use the "D" flag for the date function.
if(date('D', $unixTimestamp) === 'Mon') {
//Do what you want here
}
You can find more details on the PHP manual page for the date function here - http://php.net/manual/en/function.date.php

How to get week start date Sunday and week end date Saturday in php

How to get week start date Sunday and week end date Saturday in php.
I try my self below to code am getting Week Start Date as Monday Week End Date as Sunday.
$cdate = date("Y-m-d");
$week = date('W', strtotime($cdate));
$year = date('Y', strtotime($cdate));
$firstdayofweek = date("Y-m-d", strtotime("{$year}-W{$week}+1"));
$lastdayofweek = date("Y-m-d", strtotime("{$year}-W{$week}-7"));
Thanks,
Vasanth
Here is a solution using DateTime, which is a little bit nicer to work with than date and strtotime :) :
$today = new \DateTime();
$currentWeekDay = $today->format('w'); // Weekday as a number (0 = Sunday, 6 = Saturday)
$firstdayofweek = clone $today;
$lastdayofweek = clone $today;
if ($currentWeekDay !== '0') {
$firstdayofweek->modify('last sunday');
}
if ($currentWeekDay !== '6') {
$lastdayofweek->modify('next saturday');
}
echo $firstdayofweek->format('Y-m-d').PHP_EOL;
echo $lastdayofweek->format('Y-m-d').PHP_EOL;
Change your local configuration :
setlocale('LC_TIME', 'fr_FR.utf8');
Change 'fr_FR.utf8' by your local.
I changed my code like below. This code is correct ? but it's worked for me.
$cdate = date("Y-m-d");
$week = date('W', strtotime($cdate));
$year = date('Y', strtotime($cdate));
echo "<br>".$firstdayofweek = date("Y-m-d", strtotime("{$year}-W{$week}-0"));
echo "<br>".$lastdayofweek = date("Y-m-d", strtotime("{$year}-W{$week}-6"));

Categories