How could I decrement the date in the text box by 1 day? The value of the dateschedule is 2016-04-02. The output should be 2016-04-01 because I want to decrement it by 1 day. I tried this code:
if(isset($_REQUEST["submit"]))
{
$dateschedule=security($_POST["dateschedule"]);
echo $new_time4 = date($dateschedule, strtotime('-1 day'));
}
the format of your date function is not going to work as you expect.. see php date manual
and as also posted on another SO question, you may use this:
$new_time4 = date('Y-m-d',(strtotime( '-1 day' , strtotime( $_POST["dateschedule"]))));
You can also achieve this by using DateTime.
$date = new DateTime('2016-04-02');
$date->modify('-1 day');
echo $date->format('Y-m-d');
//Output: 2016-04-01
https://3v4l.org/mD2GR
I find this alot more readable, you can easily see what you are doing and you can change your output easily with the format funciton.
Related
I am trying to add 1 hour to a timestamp field fetched from database using the following code.
date($ls['created_at'], strtotime('+1 hour'));
However, this doesn't seem to work. It returns the same time as in database. Am I missing something? Or, is the code deprecated? What is the proper solution?
You need to give it the correct syntax to use this,
You need to send the time to change with the change itself in the function - for example (using date for wanted format):
$date = "22-02-2021 14:22:22";
echo date("d-m-Y H:i:s", strtotime($date.' +1 hour'));
This will return:
22-02-2021 15:22:22
Same as this:
echo date("d-m-Y H:i:s", strtotime("22-02-2021 14:22:22 + 1 hour"));
The idea is that you strtotime receives the date and data to change in one string like this :
echo strtotime("22-02-2021 14:22:22 + 2 hour");
Will return:
1614010942
Here I removed the Date Format so I received a unix timestamp format
I have a PHP date in a database, for example 8th August 2011. I have this date in a strtotime() format so I can display it as I please.
I need to adjust this date to make it 8th August 2013 (current year). What is the best way of doing this? So far, I've been racking my brains but to no avail.
Some of the answers you have so far have missed the point that you want to update any given date to the current year and have concentrated on turning 2011 into 2013, excluding the accepted answer. However, I feel that examples using the DateTime classes are always of use in these cases.
The accepted answer will result in a Notice:-
Notice: A non well formed numeric value encountered......
if your supplied date is the 29th February on a Leapyear, although it should still give the correct result.
Here is a generic function that will take any valid date and return the same date in the current year:-
/**
* #param String $dateString
* #return DateTime
*/
function updateDate($dateString){
$suppliedDate = new \DateTime($dateString);
$currentYear = (int)(new \DateTime())->format('Y');
return (new \DateTime())->setDate($currentYear, (int)$suppliedDate->format('m'), (int)$suppliedDate->format('d'));
}
For example:-
var_dump(updateDate('8th August 2011'));
See it working here and see the PHP manual for more information on the DateTime classes.
You don't say how you want to use the updated date, but DateTime is flexible enough to allow you to do with it as you wish. I would draw your attention to the DateTime::format() method as being particularly useful.
strtotime( date( 'd M ', $originaleDate ) . date( 'Y' ) );
This takes the day and month of the original time, adds the current year, and converts it to the new date.
You can also add the amount of seconds you want to add to the original timestamp. For 2 years this would be 63 113 852 seconds.
You could retrieve the timestamp of the same date two years later with strtotime() first parameter and then convert it in the format you want to display.
<?php
$date = "11/08/2011";
$time = strtotime($date);
$time_future = strtotime("+2 years", $time);
$future = date("d/m/Y", $time_future);
echo "NEW DATE : " . $future;
?>
You can for instance output it like this:
date('2013-m-d', strtotime($myTime))
Just like that... or use
$year = date('Y');
$myMonthDay = date('m-d', strtotime($myTime));
echo $year . '-' . $myMonthDay;
Use the date modify function Like this
$date = new DateTime('2011-08-08');
$date->modify('+2 years');
echo $date->format('Y-m-d') . "\n";
//will give "2013-08-08"
I am trying to get the next year date from a php variable which holds the date value posted from a html form.
Below is the code
$warranty_from = mysql_real_escape_string($_POST['from_date']);
Say the $warranty_from holds the value 2013-06-04. I want to get the next year date i.e
2014-06-04 and store into $warranty_to variable.
Searched lot over the net but couldnt find any resource related to my issue. Any help is appreciated. Thanks in advance.
The following should work:
$warranty_from = '2013-06-04';
$warranty_to = date('y-m-d', strtotime('+1 years', strtotime($warranty_from)));
Something like that should work
This was exact code
$newWarranty_date = date("Y-m-d",strtotime("+1 year",strtotime($warranty_from)));
Try this one
<?php
$warranty_to = date('Y-m-d', strtotime('+1 year', strtotime($warranty_from)));
?>
Calculations like this are trivial using the DateTime classes:-
$_POST['from_date'] = '2013-06-04';//Just for this example.
$warranty = DateTime::createFromFormat('Y-m-d', $_POST['from_date']);
$interval = new DateInterval('P1Y');
$warranty->add($interval);
echo $warranty->format('Y-m-d');
Will output:-
2014-06-04
i have today date like 2013-03-09 02:12:36 i want to get the output like 2012-10-09 02:12:36 (5 month ago time)
I am getting today with code
$datestring = "%Y-%m-%d: %d:%h:%i";
$time = time();
$today=mdate($datestring, $time);//gives me2013-03-09 02:12:36``
What i want is
$sixmonth= $today - 6 month//how to get this output
Please help me.
You can use a function named strtotime(), this will do what you want. You should read up about how the function works here. You should also look into the date() function to format your dates and times, you can do that so here.
echo date("Y-m-d H:i:s", strtotime("-6 months"));
Example output:
root#upstairslinux:~# php 6months.php
2012-09-09 12:20:23
root#upstairslinux:~#
I was looking at this post, and it is close to what I need:
PHP - How to count 60 days from the add date
However, in that post, the calculation is performed by adding 60 days to the current date. What I need to do is calculate the date based on a variable date (and not the current date).
Something like this:
$my_date = $some_row_from_a_database;
$date_plus_10_days = ???;
Anyone know how to do that?
Thanks
You can put something before the "+10 days" part:
strtotime("2010-01-01 +10 days");
Use date_add
http://www.php.net/manual/en/datetime.add.php
$my_date = new DateTime($some_row_from_a_database);
$date_plus_10_days = date_add($my_date, new DateInterval('P10D'));
You will have to look into strtotime(). I'd imagine your final code would look something like this:
$dateVariable = strtotime('2017-01-29');//your date variable goes here
$date_plus_60_days = date('Y-m-d', strtotime('+ 60 days', $dateVariable));
echo $date_plus_60_days;
If you are using PHP >= 5.2 I strongly suggest you use the new DateTime object. For example like below:
$date_plus_60_days = new DateTime("2006-12-12");
$date_plus_60_days->modify("+60 days");
echo $date_plus_60_days->format("Y-m-d");
I see you are retriving data from a database.
If you are using mysql you can do it on the select:
Example: you need the last date of the table and this date-7 days
select max(datefield) as ultimaf, DATE_SUB(max(datefield),INTERVAL 7 DAY) as last7
from table
It´s easy use curdate() if you want todays date.
If you need a dynamic between that selects the count of last 7 days:
select count(*) from table
where DATE_SUB(CURDATE(),INTERVAL 7 DAY)<=datefield"
date('Y-m-d H:i:s', strtotime("2014-11-24 06:33:39" +35 days"))
this will get the calculated date in defined format.
Suppose today's date is
date_default_timezone_set('Asia/Calcutta');
$today=date("Y-m-d");
And i can add 10 days in current date as follows :
$date_afte_10_days = date('Y-m-d', strtotime("$today +10 days"));