I have the following dates that I get from database as well as from php date. I did some formatting to the date from database to suit accordingly. Those are as follows
From database
$startDate = $tagQueryRows['startDate'];
$startDate = strtotime($startDate);
$startDate = date( 'd-m-Y', $startDate );
Take note that the startDate in database is in this format 2019-09-02 10:26:44
From php date
$todayDate = date('d-m-Y');
Then I did a subtraction as follows
$totalDaysCompleted = ($todayDate- $startDate);
When I did this, it shows the correct number of days but with warning
Notice: A non well formed numeric value encountered
Thus I edited my code as below
$totalDaysCompleted = (strtotime(str_replace('-','/',$todayDate)) - strtotime(str_replace('-','/',$startDate)));
This time the warning gone, but it is not showing the $totalDaysCompleted correctly. The $totalDaysCompleted should be a number like 1, 2, 3 etc. But now it is showing weird number such as -150000 etc.
Can someone help me?
After some trial and errors, I got it right by doing the following way
$startDate = $tagQueryRows['startDate'];
$startDate = strtotime($startDate);
$todayDate = date('d-m-Y');
$totalDaysCompleted = (strtotime($todayDate)) - $startDate;
$totalDaysCompleted = round($totalDaysCompleted/(60*60*24),0);
Related
I have a form with Start & End date fields. The user plugs in the dates and runs a report.
I'm trying to find the total number of days between these dates.
Currently the form fields are setup type=date. The date format is 01-01-2017.
I have tried a lot but this seems to have me pretty close.
$end_date = date("m-d-y", strtotime($endd));
$start_date = date("m-d-y", strtotime($startd));
$reportdays = ($syour_date - $eyour_date);
Any help would be appreciated.
The date function is used to format a date for printing. It cannot be used to subtract two dates. You are basically trying to subtract two date strings.
'2016-12-12' - '2017-01-12' == 'invalid!'
You should use the DateTime:::diff() function to get the difference in days like this:
$end_date = new Date($endd);
$start_date = new Date($startd);
$interval = $end_date->diff($start_date);
$reportdays = $interval->format('%d');
You can read more about this here:
http://php.net/manual/en/datetime.diff.php
I want to change date var when x days have passed
For instance:
Today is 21.12.16 - $date = '23.12.16'
Tomorrow is 22.12.16 - $date = '23.12.16'
When it's 23.12.16 - $date = '25.12.16'
Her's the code I got so far. Hope this will make some sense
$date = "2016-12-21"; //** will describe this lower
$days_passed = date_create()->diff(date_create($date))->days;
if ($days_passed >= 2){
$new_date = date('d.m.y', strtotime("+2 days"));
} else{
$new_date = $date;
}
This works ok if I just want to do it once
**I need to change this var every 2 days. I understand that i can write it to a Database or to a .txt. But there sure is a way to do this just by php
P.S. sorry for my bad English.
Here's what I came up with:
$date = '2016-12-01'; //Your script start date, you wont need to change this anymore
$everyxdate = 10; // once x days to add x to $date
$days_passed = date_create()->diff(date_create($date))->days; // passed days from start of script $date
$mod_dates = (int)($days_passed / $everyxdate); // count how much cycles have passed
$daystoadd = $mod_dates * $everyxdate + $everyxdate; // count how much days we need to add
$newdate = strtotime ("+$daystoadd day" , strtotime ( $date ) ) ; // Add needed day count to starting $date
$newdate = date ( 'd.m.y' , $newdate ); // Format date the way you want
Hope this will help some one who has the same task I had.
I'm trying to take a date and add 3 months to it for use on an accounting system and we need to split the dates into quarters for our tax return
I have the following code
$e = ORM::for_table('sys_taxdate')->find_many();
This has been input into the database using a date type in the column and displys correctly as :
2016-07-02
I then want to add 3 months to this date so i can search the database for any invoices rasied between these 2 dates. The code i have tried is
$idate = $e;
$its = strtotime($idate);
$dd = date('Y-m-d', strtotime('+3 months', $its));
But $dd outputs the date 1970-04-01
I have looked over many posts and looks as though strtotime is not starting with the right date format, or at least thats what i think but have been trying for hours now and have hit a brick wall
I figured it out.
I ended up getting the exact result from the database first
$e = ORM::for_table('sys_taxdate')->find_one(2);
$date = $e->get('taxdate');
$idate = $date;
$its = strtotime($idate);
$dd = date('Y-m-d', strtotime('+3 months', $its));
Below code is used to get last and next Saturday of current week
// GET Last Satuday of current week
$dt_week_start_time = strtotime("last saturday")+((20*3600)+ 1);
$dt_week_start_date = date('Y-m-d G:i:s', $dt_week_start_time);
// GET Last Satuday of current week
$dt_week_end_time = $dt_week_start_time + (7*3600*24) - 1;
$dt_week_end_date = date('Y-m-d G:i:s', $dt_week_end_time);
Below code is used to convert above both date related variables to EST timezone (default timezone is UTC)
$est_time = new DateTimeZone('EST');
$datetime = new DateTime($dt_week_start_date);
$datetime->setTimezone($est_time);
$dt_week_start_date = $datetime->format('Y-m-d G:i:s');
$datetime = new DateTime($dt_week_end_date);
$datetime->setTimezone($est_time);
$dt_week_end_date = $datetime->format('Y-m-d G:i:s');
Now I want to compare these EST timezone dates with dates in mySQL database table. Dates in mySQL database table are stored in UTC timezone by default. By studying on internet and someone suggested to use CONVERT_TZ function. I tried it but it is giving error like mentioned below
What is wrong in my query? Please advise.
Below 2 queries giving this error: 'Notice: Undefined index: purchasedatetime ...'
$str_query_select = "SELECT CONVERT_TZ(purchasedatetime, 'UTC', 'EST' ) FROM t_product_purchase WHERE sellerpkid=1";
$str_query_select = "SELECT CONVERT_TZ((SELECT purchasedatetime FROM t_product_purchase),'UTC','EST') FROM t_product_purchase WHERE sellerpkid=1";
I am trying to change a date displayed in this format "d/m/Y" into this format "d-m-Y", and I wrote the code below:
//Add $license years to today
$today = date("j-m-Y");
$license = $row_customize['license'];
$startdate = $row_customize['startdate'];
list($d,$m,$Y) = explode('/',$startdate);
$newstartDate = $d."-".$m."-".$Y;
$enddate= date("j-m-Y", strtotime("$newstartDate +$license years"));
echo $today;
echo $enddate;
$today displayed correctly, but $enddate is showing me a date in 1970 e.g 1-01-1970.
Please what have I done wrong?
I would put
$newstartDate = $d."-".$m."-".(intval($Y)+intval($license));
$enddate= date("j-m-Y", strtotime($newstartDate));
I think the format "<date> + n years" is not allowed for strtotime()
The default date is 0 which equates to 01-01-1970. So both $newstartDate and $license years are 0 so when added together the result is still 0. So the questions are why is $newstartDate and $license set to 0.
I think like p91paul said you may not be able to use n Years. I think also $newstartDate may be in an unsupported format. Could you use $startDate in the function and then perform the format change after?