I have a static date 28-04-2012,. I want to update the only the month when the date gets to the 28th of the new month. So if the date is 28-05-2012, then 28-06-2012, 28-07-2012 and so on.
I would also like to update the year when I reach 2013. But always keep the date at 28? Can someone advise me how this would be done using js or php?
$current_date = "28-04-2012";
$next_month = strtotime($current_date . "+1month");
echo date('Y-m-d', $next_month);
the most simple trick i guess.
<?php
$date = '28-04-2012';
echo date('j-m-Y', strtotime("1 month", strtotime($date))) . "\n";
?>
See here: http://www.roseindia.net/tutorial/php/phpdate/php-date-add-1-month.html
$todayDate = date("Y-m-d");// current date
echo "Today: ".$todayDate."<br>";
//Add one day to today
$dateOneMonthAdded = strtotime(date("Y-m-d", strtotime($todayDate)) . "+1 month");
echo "After adding one month: ".date('l dS \o\f F Y', $dateOneMonthAdded)."<br>";
People are suggesting strtotime(), but I have to say I have a strong dislike for this function. It isn't designed for date arithmetic, but it was the only option available in older versions of PHP, so everyone uses it for that.
A better solution can be found in the PHP5 DateTime class, and in particular the DateTime::Add method (also known as the date_add() function).
The only reason to use strtotime() instead ofDateTime::Add()is if you're using a version of PHP older than 5.3, since this is when theAdd` method was added. But if that's the case, your first priority should be to upgrade your PHP.
Related
I have a problem here I want to get the +4 week date from the current date using carbon, the +4 week plan will be dynamic depending on the input entered by the user, how do I make it, I've tried using this code but it's time to step back
$dt = Carbon::now();
dd($dt->week(4)->format('Y-m-d'));
Check Carbon docs, you may use addWeeks():
$dt = Carbon::now();
dd($dt->addWeeks(4)->format('Y-m-d'));
The week() method you've used, sets the week number using given first day of week and first day of year included in the first week.
I am not sure to understand your question, but I guess you just have to use :
$dt = Carbon::now();
$dt->addWeeks(4);
dd($dt->format('Y-m-d');
You don't need carbon for such simple tasks.
With DateTime
echo date_create('+4 weeks')->format("Y-m-d");
or with date and strtotime
echo date("Y-m-d",strtotime('+4 weeks'));
I know I can do
$threemonthsago = date("Y-m-d", strtotime("-3 month"));
echo "$threemonthsago";
However I want to get 3.5 months ago not three months ago. So I tried to do
$threemonthsago = date("Y-m-d", strtotime("-3.5 month"));
echo "$threemonthsago";
However it does not seem to give me the correct date its giving me like September something which it should not be since its currently April.
The decimal throws off strtotime() as that is not a valid format it recognizes. The real issue you have is what exactly is half of a month? If you traverse February it gets really dicey.
This is somewhat easier to do using DateTime() and DateInterval() if you specify exactly what half of a month is:
$date = new DateTime();
$new_date = $date->sub(new DateInterval('P3M15D'));
echo $new_date->format('Y-m-d');
Demo
echo(strtotime("-105 days"));
I have this invoice with todays date:
I want to display the expiration date (30 days later), is there a function that allows this?
or is it something simple like adding a +30 somewhere?
Help :D
$date = date();
$future = date_add($date, date_interval_create_from_string('30 days'));
is the procedural way to do it. There's also an OOP version documented here as well.
You can use a combination of date() and strtotime().
echo date('Y-m-d', strtotime('+30 days'));
For more information on specifying date formats, see the manual page for date().
I hope it will help you.
$thirtydaysadd = mktime(0, 0, 0, date("m"), date("d")+1, date("y"));
echo "That day will be ".date("m/d/y", $thirtydaysadd);
mktime() is used to create new time stamp.
strtotime is probably your best bet.
echo strtotime("+30 days"), "\n";
http://php.net/manual/en/function.strtotime.php
When you face such problems, PHP online documentation is very useful.
Just for to http://php.net/keyword (for example, http://php.net/date) and documentation page for that keyword will be displayed. You could see Date/Time functions in the left sidebar which has the link to function date_add
I am making a invoices page and I want the date to appear automatically as an input as well as the date of expiration (30 days later)
is there an automatic function?
I have this:
TD
can value change to something like todaysdate??? or 30 days later??
help! :D
You need to use DateTime::add method.
<?php
$date = new DateTime('2011-01-01');
$date->add(new DateInterval('P30D'));
echo $date->format('Y-m-d') . "\n";
?>
Your question is extremely difficult to decipher.
Perhaps you are looking for PHP's strtotime() function. For example, strtotime('today') returns a timestamp for today's date, and strtotime('next Wednesday') returns a timestamp for next Wednesday.
I am trying to use php datetime object for handling dates.
Here is my code:
$date = new DateTime('01 Dec, 1969');
echo $date->format('Y-m-d');
The above code returns 2010-12-01
But If I change year from 1969 to 1945 or anything less than 1960 then the code returns incorrect year. For example:
This code:
$date = new DateTime('01 Dec, 1950');
echo $date->format('Y-m-d');
returns
2010-12-01
This is likely a bug. Consider filing it to the bugtracker.
When you change the input format to
$date = new DateTime('Dec 1st, 1950');
echo $date->format('Y-m-d');
PHP will correctly make this into
1950-12-01
See http://codepad.org/trFfB6Q1
As of PHP5.3, you can also use DateTime::createFromFormat to create a date. This would work with your original DateTime string then:
$date = DateTime::createFromFormat('d M, Y', '01 Dec, 1950');
echo $date->format('Y-m-d');
See http://codepad.viper-7.com/08kK5M
Given that this problem does not occur on my system (PHP5.3 on a windows machine)
I suggest you update to php 5.3.
There are no drawbacks and this is probably not the only bug you will run into.
I have tested different date formats('1969/12/1','01 Dec, 1969',..) and had no problems at all.
if the problem persist feel free to slap me ;)
PHP's Datetime is based on a unix timestamp which started counting from 1st of january 1970.
You cannot use DateTime to acces a date before that.