I am trying to subtract X days from a date retrieved from MySQL.
X is a variable that can be any integer.
I tried:
$days = trim(explode("days",explode("Order will be late by",$resPO['EXCEPTION_MSG'])[1])[0]); // returning a number 1, 2, 3, etc
$date = $resPO['DUE_DESIRED_RECV_DATE']; //date from database
$reqdate = date('Y-m-d', strtotime("-$days days", strtotime($date)));
Resulting: 04/30/2021
No matter what the date is inserted into the formula. HOWEVER, when I manually change the formula to a constant, it does calculate properly.
Example:
$reqdate = date('Y-m-d', strtotime("-3 days", strtotime($date)));
EDIT:
The $days is working fine when I use a manual date like "2021-04-30". So, looks like the problem is the date I am getting from MySQL.
It is set as DATE and is printing properly.
EDIT 2:
// FORMATTING DATE
$days = $resPO['ORDER_PROJ_LATE_BY'];
//$days = 3;
$date = $resPO['DUE_DESIRED_RECV_DATE']; //date from database
$new_date = explode("-",$date);
$var_new_date = date("Y-m-d", mktime(0,0,0,$new_date[1],$new_date[2] - $days,$new_date[0]));
$newdate = date('Y-m-d', strtotime("-$days days", strtotime($var_new_date)));
EDIT 3:
I found a workaround by multiplying the DAYS (int) from MySQL by 3 and then dividing the results by 3.1 (closer as possible to 3).
// FORMATTING DATE
$days = $resPO['ORDER_PROJ_LATE_BY'];
$days = ($days * 3)/3.1;
Any help?
your code is alright, can it be that the date read from MySQL - in $resPO['DUE_DESIRED_RECV_DATE'] - has an unconventional formatting? strtotime expects them to be formatted in one of the recognized formats.
$days = 3;
$date = '2021-05-01';
$reqdate = date('Y-m-d', strtotime("-$days days", strtotime($date)));
echo "$reqdate\n";
returns
2021-04-28
Related
I'm trying to add two weeks to sql result that comes back as 16/11/2016. When I do something like
$twoweeks = strtotime($time_db);
$expiry_date = $twoweeks;
$date = strtotime($expiry_date);
$date = strtotime("+14 day", $date);
echo date('d/m/y', $date);
I keeping getting 15/01/70... any ideas?
You made a mistake on line 4 by putting a number variable as a first parameter of strtotime. strtotime expects a string of a valid date/time format as a first parameter otherwise it returns FALSE.
How I think your code should be:
$twoweeks = strtotime($time_db);
$date = strtotime("+ 2 weeks", $twoweeks);
echo date('d/m/y', $date);
Or maybe even:
echo date('d/m/y', strtotime($time_db . ' + 2 weeks'));
You can use
$numberOfWeeks = 2;
$newTime = strtotime($time_db) + ($numberOfWeeks * 60 * 60 * 24 * 7);
or you can do directly in (mysql) select
select date_add( your_column, INTERVAL 2 WEEK) from my_table;
What's happening here is that your 16/11/2016 is day-month-year and the slashes are an issue.
Had your date been 11/16/2016, you would have found that it would have been OK.
You need to convert/replace those to dashes/hyphens.
$time_db = "16/11/2016";
$time_db = str_replace('/', '-', $time_db);
$two_weeks_later = date('d-m-Y',strtotime($time_db . "+14 days"));
// or display as Year-month-day
// $two_weeks_later = date('Y-m-d',strtotime($time_db . "+14 days"));
echo $two_weeks_later;
When working with dates (and times), it's best to use the built-in MySQL date/time functions, rather than storing them as plain text; it's a lot less trouble and much easier when querying.
Reference:
http://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html
$date = date('Y-m-d H:i:s',time());
$date = strtotime($date);
$date = strtotime("+14 day", $date);
$valuedate= date('Y-m-d H:i:s',$date);
Try this
I need to calculate difference between 2 dates here is my dates
$start = strtotime('17/05/2016');
$end = strtotime('12/05/2016');
I have tried
echo $days_between = ceil(abs($end - $start) / 86400);
But it shows output as 17140
Hep to find the number of days between 2 given dates
You just need to specify what is the date format, so:
$date1 = DateTime::createFromFormat('d/m/Y',"12/05/2016");
$date2 = DateTime::createFromFormat('d/m/Y',"17/05/2016");
echo $diff = $date2->diff($date1)->format("%a"); //output: 5
You need to change the format:
with d/m/Y at m/d/Y
You can also use a format like this:
$s = DateTime::createFromFormat('d/m/Y', '17/05/2016');
$d = DateTime::createFromFormat('d/m/Y', '05/12/2016');
And get the difference of days:
$start = DateTime::createFromFormat('d/m/Y', '17/05/2016');
$end = DateTime::createFromFormat('d/m/Y', '12/05/2016');
$interval = $end->diff($start);
$days = $interval->format('%a');
You are using a wrong format for dates, the original is: "Y-m-d" not "d/m/Y", you can use something like this if you could change the date format:
$now = strtotime("2016-05-17"); // or your date as well
$your_date = strtotime("2016-05-12");
$datediff = $now - $your_date;
echo floor($datediff/(60*60*24));
Hi I'm not sure how get this equation correct because I not sure about the dates. I know it returns the same date but I'm trying to learn different formats and how to add dates different days. I know may not be the best way of writing the code I still would like to know how to write this code. The equation is :
$mhYr + ($vSDday -1day)
My code:
$date = "2015-11-02";
$vStartDate = strtotime($date);
$currentdate = $vStartDate;
$vSDday = date('d', $currentdate); //day
$monthY = date('m', $currentdate); //Month
$mhYr = date("01-$monthY"); //first day of any month
But I keep getting errors because of the dates.
An example would be 1stOfMonth + (daysOfStartDate -1 day). So if I started on the 2 November i want to perform : 1st Nov + (2 - 1day). But I don't know how to put all together.
When you do :
$date = "2015-11-02";
$vStartDate = strtotime($date);
and try to print it ( echo $vStartDate), it will print number of seconds difference between 1 january 1970 at midnight and 2015-11-02 , to convert the time to human readable format you need getdate($vStartDate ), it will return collection format.
$date = "2015-11-02";
$vStartDate = strtotime($date);
$timeCollection= getDate($vStartDate);
Then you can start doing :
$day = $timeCollection["mday"]; //day
$month = $timeCollection["mon"]; // month
$year = $timeCollection["year"]; // year
$hour = $timeCollection["hours"]; // hours
$minute = $timeCollection["minutes"]; // minutes
$seconds = $timeCollection["seconds"]; // seconds
I am trying to check how many days were passed since the user last entered the system. I get the last time he\she entered from mysql table column (datetime). so I wrote :
$user_last_visit = $user_info['last_updated']; // 2013-08-08 00:00:00
$user_last_visit_str = strtotime($user_last_visit); // 1375912800
$today = strtotime(date('j-m-y')); // 1250114400
$diff = $today - $user_last_visit_str;
Where $user_info['last_updated'] has the last time he\she visited with the value of 2013-08-08 00:00:00.
After strtotime I get $user_last_visit_str equals to 1375912800
$today has the value of 9-08-13 and after strtotime I get 1250114400.
Some reason $diff = $today - $user_last_visit_str; is negative instead of getting a positive value with 24*60*60*1000 (one day = 24*60*60*1000 ms).
Any ideas?
A simple solution using diff:
echo date_create()->diff(date_create($user_last_visit))->days;
If all else fails, just do:
$diff = floor((time() - $user_last_visit_str) / (60 * 60 * 24));
you can use below code to get date diff, here i given static last date which is 15th july 2013, and taking different from current date.
$last_date = date('y-m-d', strtotime('15th july 2013'));//here give your date as i mentioned below
//$last_date = date('y-m-d', strtotime($your_old_date_val));
$current_date = date('y-m-d');//
echo $date_diff = strtotime($current_date) - strtotime($last_date) ;
echo $val = 60*60*24;
$days_diff = $date_diff / $val;
echo $days_diff;
Try this:
$user_last_visit = $user_info['last_updated']; // considering the value of user last visit is 2013-08-08 00:00:00 which indicates year, month, day, hour, minute, second respectiveliy
$user_last_visit_str = strtotime($user_last_visit);
$today = time();
$diff = $today - $user_last_visit_str;
$no_of_days = ($diff / (60*60*24));
Try using a DateTime object if your version of PHP supports it:
$user_last_visit = DateTime::createFromFormat('Y-m-d H:i:s', $user_info['last_updated']);
$today = new DateTime();
$diff = $today->diff( $user_last_visit, true );
echo $diff->days;
$diff will be a DateInterval object.
try reversing the $today assignment, like so:
$today = strtotime(date('y-m-j'));
that worked for me.
I have data in my database that returns the month/day/year data points for events.
What I want to do is check whether today is 20 days after the month/day/year that I get.
So far I have something like:
// Get date data
$day = $row['DAYOFMONTH(hike_date)'];
$year = $row['YEAR(hike_date)'];
$month = $row['MONTH(hike_date)'];
// Get today's date
$todays_date = date("Y-m-d");
// Create the date I am comparing with
$date_string = $year.'-'.$month.'-'.$day;
My question is how do I compare it? And is the format going to matter in comparing the two dates?
Thanks!
Parse the month/day/year into a DateTime:
$other = date_create($year.'-'.$month.'-'.$day);
Add 20 days to it:
$twenty_days_after = clone $other;
$twenty_days_after->modify('+20 days');
Compare it with today:
if ($today >= $twenty_days_after) {
// today is 20 days after the month/day/year that you got
}
See date_create.
$database_date = strtotime(sprintf('%s-%s-%s', $year,$month,$day));
$twenty_days_from_now = strtotime('+20 days');
$twenty_days_From_database = strtotime(sprintf('%s-%s-%s +20 days', $year,$month,$day));
Using strttime is the easiest method.
use
strtotime()
function which will convert your dates into timestamp and you can compare them easier
// Get date data
$day = $row['DAYOFMONTH(hike_date)'];
$year = $row['YEAR(hike_date)'];
$month = $row['MONTH(hike_date)'];
// Get today's date
$todays_date = date("Y-m-d");
// Create the date I am comparing with
$date_string = $year.'-'.$month.'-'.$day;
if ( date( 'Y-m-d', strtotime( "+20 days", $date_string ) ) {
//date is 20 days before today
}
strtotime does magical things