strftime adding +1 day also removes 5 minutes - php

Somehow, using strtotime and adding "+1 day" not only adds the day, but also removes 5 minutes.
In the following example I expect '2013-10-02 08:15:00', but get '2013-10-02 08:10:00':
$myDate = '2013-10-01 08:15:00';
$newDate = strtotime($myDate . ' +1 day');
$newDate = strftime("%Y-%m-%d %H:%m:00", $newDate);
debug($newDate);
//'2013-10-02 08:10:00'
BUT - if I use date() instead of strftime(), it works fine
$myDate = '2013-10-01 08:15:00';
$newDate = strtotime($myDate . ' +1 day');
$newDate = date("Y-m-d H:i:s", $newDate);
debug($newDate);
//'2013-10-02 08:15:00'

Needed a capital M instead of m.
Check http://php.net/manual/en/function.strftime.php
$myDate = '2013-10-01 08:15:00';
$newDate = strtotime($myDate . ' +1 day');
$newDate = strftime("%Y-%m-%d %H:%M:00", $newDate);
debug($newDate);

Related

How to get the current date in PHP, and add 1 month to the current date?

I'm coding a script where I require to save the current date, and the date 1 month from that date. I am pretty sure that the time() variable works, but I am not sure how to +1 month onto that?
Any ideas, suggestions. Cheers!
Try this
$today = date("Y-m-d");
$date = date('Y-m-d', strtotime('+1 month', $today));
or use DateTime()
$dt1 = new DateTime();
$today = $dt1->format("Y-m-d");
$dt2 = new DateTime("+1 month");
$date = $dt2->format("Y-m-d");
$time = strtotime("2010-12-11");
$final = date("Y-m-d", strtotime("+1 month", $time));
(OR)
strtotime( "+1 month", strtotime( $time ) );
this returns a timestamp that can be used with the date function
Use this:
Current Date:
echo "Today is " . date("Y/m/d");
1 Month to the Current Date:
$time = strtotime(date("Y/m/d"));
$final = date("Y-m-d", strtotime("+1 month", $time));
<?php
$current_time = date("Y-M-d h:i:s",time()); // Getting Current Date & Time
print $current_time; // Current Date & Time Printing for display purpose
$future_timestamp = strtotime("+1 month"); // Getting timestamp of 1 month from now
$final_future = date("Y-M-d h:i:s",+$future_timestamp); // Getting Future Date & Time of 1 month from now
print $final_future; // Printing Future time for display purpose
?>
shorter : $today=date("Y-m-d"); $date=
This one liner worked for me:
$monthFromToday = date("Y-m-d", strtotime("+1 month", strtotime(date("Y/m/d"))));
The given answers may not give you the results you might expect or desire.
Consider:
$today = "29Jan2018";
$nextMonth = date('dMY', strtotime('+1 month', (strtotime($today))));
echo $today // yields 29Jan2018
echo $nextMonth // yields 01Mar2018
$today = date("Y-m-d");
$enddate = date('Y-m-01',strtotime($today. ' + 1 months'));
You could also consider using the Carbon package.
The solution would look like this:
use Carbon\Carbon
$now = Carbon::now;
$now->addMonth();
Here is the link for reference https://carbon.nesbot.com/docs/

Adding days to date in php

I read about this but does not working for me. Here is my code:
$today = date_create()->format("d/m/Y"); // Today is 25/04/2013
$num_days = GetNumberOfdays();
$end_date = date("d/m/Y", strtotime($today . " + $num_days days"));
The value that I get from $end_date is 31/12/1969. What am I doing wrong?
Try this instead:
$end_date = date("d/m/Y", strtotime("+ $num_days days", time()));
EDIT: I changed the $today variable to just time() which is essentially getting you the same information if you're just looking for today's date.
From what it looks like you're trying to do, you don't even need $today (as it defaults to now if date is not supplied), so you could just do eg:
$end_date = date("d/m/Y", strtotime("+ 5 days"));
echo $end_date;
result would be
30/04/2013
if you want to provide a date, you need the parameters the other way round, as per the manual:
strtotime ( string $time [, int $now = time() ] )
date_create() return a DateTime object.
You could use DateTime::modify method.
$date = new \DateTime(); // Defaults to Today
$num_days = 123;
$date->add(
new \DateInterval('P' . $num_days . 'D')
);
echo $date->format('d-M-Y');
$today = date_create()->format("d/m/Y"); // Today is 25/04/2013
$num_days = date_create()->format("d");
echo $end_date = date("d/m/Y", strtotime(" + $num_days days"));
<?
// note change of $today format
$today = date_create()->format("d-m-Y"); // Today is 25-04-2013
$num_days = GetNumberOfdays();
$end_date = date("d/m/Y", strtotime("+" . $num_days . " days", strtotime($today)));
?>

Need to add weeks, days and months in a date for a package subscription

I am currently working on a package manager for my current project and I need to add package subscription date and package expiry date.
I am using php function date("Y-m-d H:i:s") i.e
$package_subscription_start_date = date("Y-m-d H:i:s");
now here is my requirement:
I need to have packages such as for 1 day, 2 days, 5 days, 1 week, 4 weeks, 12 weeks, 1 month, 6 months, 9 months, 1 year, 2 years, 5years and 10 years.
How is it possible for me to add these values and calculate the package expiry date for inserting into mysql, something like
$package_expiry_date = date("Y-m-d H:i:s") + 1 day;
$package_expiry_date = date("Y-m-d H:i:s") + 5 days;
$package_expiry_date = date("Y-m-d H:i:s") + 1 week;
$package_expiry_date = date("Y-m-d H:i:s") + 4 weeks;
$package_expiry_date = date("Y-m-d H:i:s") + 2 months;
$package_expiry_date = date("Y-m-d H:i:s") + 2 years;
Lastly I need to take care of other date issues like valid date and leap year etc.
Also I need this to be done at php level, not using mysql / sql query.
I will appreciate any help in this direction.
Something like
$date=date('Y-m-d H:i:s', strtotime('+1 day'));
$date=date('Y-m-d H:i:s', strtotime('+5 days'));
$date=date('Y-m-d H:i:s', strtotime('+1 week'));
$date=date('Y-m-d H:i:s', strtotime('+4 weeks'));
$date=date('Y-m-d H:i:s', strtotime('+2 months'));
$date=date('Y-m-d H:i:s', strtotime('+2 years'));
Have you tried the strtotime() command in php?
echo $expires = strtotime(date("Y-m-d H:i:s")." + 1 day");
echo date("Y-m-d H:i:s", $expires);
Gives you the output of
135082420020
12-10-21 08:56:40
Have a look at the php DateTime object.
Eg to add time on to a date:
//Get the current date
$date = new DateTime();
//Add 1 day
$date->modify('+1 Day');
//Output in Y-m-d format
echo $date->format('Y-m-d');
It has lots of other useful functions
<?php
echo $date = date("Y-m-d");// current da
$date1 = strtotime(date("Y-m-d", strtotime($date)) . " +1 day");
echo date("Y-m-d", $date1);
echo date("Y-m-d", $date1);
$date2 = strtotime(date("Y-m-d", strtotime($date)) . " +1 week");
echo date("Y-m-d", $date2);
$date3 = strtotime(date("Y-m-d", strtotime($date)) . " +2 week");
echo date("Y-m-d", $date3);
$date4 = strtotime(date("Y-m-d", strtotime($date)) . " +1 month");
echo date("Y-m-d", $date4);
$date5 = strtotime(date("Y-m-d", strtotime($date)) . " +1 year");
echo date("Y-m-d", $date5);
?>

How to add 5 minutes to current datetime on php < 5.3

I want to add 5 minutes to this date: 2011-04-8 08:29:49
$date = '2011-04-8 08:29:49';
When I use strtotime I am always getting 1970-01-01 08:33:31
How do I add correctly 5 minutes to 2011-04-8 08:29:49?
$date = '2011-04-8 08:29:49';
$currentDate = strtotime($date);
$futureDate = $currentDate+(60*5);
$formatDate = date("Y-m-d H:i:s", $futureDate);
Now, the result is 2011-04-08 08:34:49 and is stored inside $formatDate
Enjoy! :)
Try this:
echo date('Y-m-d H:i:s', strtotime('+5 minutes', strtotime('2011-04-8 08:29:49')));
$expire_stamp = date('Y-m-d H:i:s', strtotime("+5 min"));
$now_stamp = date("Y-m-d H:i:s");
echo "Right now: " . $now_stamp;
echo "5 minutes from right now: " . $expire_stamp;
Results in:
2012-09-30 09:00:03
2012-09-30 09:05:03
$date = '2011-04-8 08:29:49';
$newDate = date("Y-m-d H:i:s",strtotime($date." +5 minutes"))
For adding
$date = new DateTime('2014-02-20 14:20:00');
$date->add(new DateInterval('P0DT0H5M0S'));
echo $date->format('Y-m-d H:i:s');
It add 5minutes
For subtracting
$date = new DateTime('2014-02-20 14:20:00');
$date->sub(new DateInterval('P0DT0H5M0S'));
echo $date->format('Y-m-d H:i:s');
It subtract 5 minutes
If i'm right in thinking.
If you convert your date to a unix timestamp via strtotime(), then just add 300 (5min * 60 seconds) to that number.
$timestamp = strtotime($date) + (5*60)
Hope this helps
more illustrative for simple and clear solution
$date = '2011-04-8 08:29:49';
$newtimestamp = strtotime($date. ' + 5 minute');//gets timestamp
//convert into whichever format you need
$newdate = date('Y-m-d H:i:s', $newtimestamp);//it prints 2011-04-08 08:34:49

Adding days to $Date in PHP

I have a date returned as part of a MySQL query in the form 2010-09-17.
I would like to set the variables $Date2 to $Date5 as follows:
$Date2 = $Date + 1
$Date3 = $Date + 2
etc., so that it returns 2010-09-18, 2010-09-19, etc.
I have tried
date('Y-m-d', strtotime($Date. ' + 1 day'))
but this gives me the date before $Date.
What is the correct way to get my Dates in the format form 'Y-m-d' so that they may be used in another query?
All you have to do is use days instead of day like this:
<?php
$Date = "2010-09-17";
echo date('Y-m-d', strtotime($Date. ' + 1 days'));
echo date('Y-m-d', strtotime($Date. ' + 2 days'));
?>
And it outputs correctly:
2010-09-18
2010-09-19
If you're using PHP 5.3, you can use a DateTime object and its add method:
$Date1 = '2010-09-17';
$date = new DateTime($Date1);
$date->add(new DateInterval('P1D')); // P1D means a period of 1 day
$Date2 = $date->format('Y-m-d');
Take a look at the DateInterval constructor manual page to see how to construct other periods to add to your date (2 days would be 'P2D', 3 would be 'P3D', and so on).
Without PHP 5.3, you should be able to use strtotime the way you did it (I've tested it and it works in both 5.1.6 and 5.2.10):
$Date1 = '2010-09-17';
$Date2 = date('Y-m-d', strtotime($Date1 . " + 1 day"));
// var_dump($Date2) returns "2010-09-18"
From PHP 5.2 on you can use modify with a DateTime object:
http://php.net/manual/en/datetime.modify.php
$Date1 = '2010-09-17';
$date = new DateTime($Date1);
$date->modify('+1 day');
$Date2 = $date->format('Y-m-d');
Be careful when adding months... (and to a lesser extent, years)
Here is a small snippet to demonstrate the date modifications:
$date = date("Y-m-d");
//increment 2 days
$mod_date = strtotime($date."+ 2 days");
echo date("Y-m-d",$mod_date) . "\n";
//decrement 2 days
$mod_date = strtotime($date."- 2 days");
echo date("Y-m-d",$mod_date) . "\n";
//increment 1 month
$mod_date = strtotime($date."+ 1 months");
echo date("Y-m-d",$mod_date) . "\n";
//increment 1 year
$mod_date = strtotime($date."+ 1 years");
echo date("Y-m-d",$mod_date) . "\n";
You can also use the following format
strtotime("-3 days", time());
strtotime("+1 day", strtotime($date));
You can stack changes this way:
strtotime("+1 day", strtotime("+1 year", strtotime($date)));
Note the difference between this approach and the one in other answers: instead of concatenating the values +1 day and <timestamp>, you can just pass in the timestamp as the second parameter of strtotime.
Here has an easy way to solve this.
<?php
$date = "2015-11-17";
echo date('Y-m-d', strtotime($date. ' + 5 days'));
?>
Output will be:
2015-11-22
Solution has found from here - How to Add Days to Date in PHP
Using a variable for Number of days
$myDate = "2014-01-16";
$nDays = 16;
$newDate = strtotime($myDate . '+ '.$nDays.' days');
echo new Date('d/m/Y', $newDate); //format new date
Here is the simplest solution to your query
$date=date_create("2013-03-15"); // or your date string
date_add($date,date_interval_create_from_date_string("40 days"));// add number of days
echo date_format($date,"Y-m-d"); //set date format of the result
This works. You can use it for days, months, seconds and reformat the date as you require
public function reformatDate($date, $difference_str, $return_format)
{
return date($return_format, strtotime($date. ' ' . $difference_str));
}
Examples
echo $this->reformatDate('2021-10-8', '+ 15 minutes', 'Y-m-d H:i:s');
echo $this->reformatDate('2021-10-8', '+ 1 hour', 'Y-m-d H:i:s');
echo $this->reformatDate('2021-10-8', '+ 1 day', 'Y-m-d H:i:s');
To add a certain number of days to a date, use the following function.
function add_days_to_date($date1,$number_of_days){
/*
//$date1 is a string representing a date such as '2021-04-17 14:34:05'
//$date1 =date('Y-m-d H:i:s');
// function date without a secrod argument returns the current datetime as a string in the specified format
*/
$str =' + '. $number_of_days. ' days';
$date2= date('Y-m-d H:i:s', strtotime($date1. $str));
return $date2; //$date2 is a string
}//[end function]
All have to use bellow code:
$nday = time() + ( 24 * 60 * 60);
echo 'Now: '. date('Y-m-d') ."\n";
echo 'Next Day: '. date('Y-m-d', $nday) ."\n";
Another option is to convert your date string into a timestamp and then add the appropriate number of seconds to it.
$datetime_string = '2022-05-12 12:56:45';
$days_to_add = 1;
$new_timestamp = strtotime($datetime_string) + ($days_to_add * 60 * 60 * 24);
After which, you can use one of PHP's various date functions to turn the timestamp into a date object or format it into a human-readable string.
$new_datetime_string = date('Y-m-d H:i:s', $new_timestamp);

Categories