php MySQL Get Missing day from a Month or BETWEEN days - php

I need to get an array with days missing from a month.
Get year and month: to Between:
$dateS //have the current given date to make the filter
$year = mb_substr($dateS, 0, 4);
$month = mb_substr($dateS, 5, 7);
$tday = cal_days_in_month(CAL_GREGORIAN, (int)$month, (int)$year);
$cmonthS = $year."-".$month."-01 00:00:00";
$cmonthE = $year."-".$month."-".$tday." 23:59:59";
I am getting the array with this statement:
$stmtpre = "SELECT days FROM registry WHERE days BETWEEN '2016-12-01 00:00:00' AND '2016-12-31 00:00:00';";
All data have the current format as date time in MySQL:
XXXX-XX-XX 00:00:00
But it returns the current day, and I need to get in PHP the missing days.
Does anyone know how can i do it?

i create and array with Total days of the month and other array with de Event day i run this function to get the diference into this 2 array:
http://php.net/manual/en/function.array-diff.php
and get finaly my No Event Array

Related

php future date showing 1970-04-01 instead of 3 months after start date

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));

Calculate end date pushing date forward if there is a holiday

I have written some code where I have a start date and number of days duration and then get an end date ie start date 18th December, duration 21 days, end date 8th January. However I want to push the end date forward to avoid certain holidays (25th December through to 6th January) so that the end date becomes 15th January. All the answers I have seen include how to calculate business days and take out weekends, which I don't need. I just want to be able to define specific holidays in an array, get it to see if the holiday is within the start date and end date and if it is move the end date forward by that number of days.
Oh, then the date needs to be inserted into a database. Thanks.
Iterate through the $holidays array. If a holiday is in the given range, then move end date one day in the future:
$startDate = new DateTime('2015-12-18');
$endDate = new DateTime('2016-01-08');
// $holidays array must be sorted. if not, then sort it first
$holidays = array("2015-12-25","2015-12-26","2016-01-01");
$newEndDate = $endDate;
foreach ($holidays as $holiday) {
$holidayDate = new DateTime($holiday);
if ($startDate <= $holidayDate && $holidayDate <= $newEndDate) {
// there is a holiday in the range
$newEndDate->add(new DateInterval('P1D'));
}
}
echo($newEndDate->format('Y-m-d'));
Thank you for all those that posted possible solutions. I solved the problem by adding a selector to the php page which adds 7, 14 or 21 days to the end date (the user sees this as 1 week, 2 weeks etc) and this solved the problem.

How to determine if a date is more than three months past current date

I am getting a date back from a mysql query in the format YYYY-MM-DD.
I need to determine if that is more than three months in the past from the current month.
I currently have this code:
$passwordResetDate = $row['passwordReset'];
$today = date('Y-m-d');
$splitCurrentDate = explode('-',$today);
$currentMonth = $splitCurrentDate[1];
$splitResetDate = explode('-', $passwordResetDate);
$resetMonth = $splitResetDate[1];
$diferenceInMonths = $splitCurrentDate[1] - $splitResetDate[1];
if ($diferenceInMonths > 3) {
$log->lwrite('Need to reset password');
}
The problem with this is that, if the current month is in January, for instance, giving a month value of 01, and $resetMonth is November, giving a month value of 11, then $differenceInMonths will be -10, which won't pass the if() statement.
How do I fix this to allow for months in the previous year(s)?
Or is there a better way to do this entire routine?
Use strtotime(), like so:
$today = time(); //todays date
$twoMonthsLater = strtotime("+3 months", $today); //3 months later
Now, you can easily compare them and determine.
I’d use PHP’s built-in DateTime and DateInterval classes for this.
<?php
// create a DateTime representation of your start date
// where $date is date in database
$resetDate = new DateTime($date);
// create a DateIntveral representation of 3 months
$passwordExpiry = new DateInterval('3M');
// add DateInterval to DateTime
$resetDate->add($passwordExpiry);
// compare $resetDate to today’s date
$difference = $resetDate->diff(new DateTime());
if ($difference->m > 3) {
// date is more than three months apart
}
I would do the date comparison in your SQL expression.
Otherwise, PHP has a host of functions that allow easy manipulation of date strings:
PHP: Date/Time Functions - Manual

php find week number from specific date

I want to find a specific week number from the specific start date. For example $date is dragged from the database (i.e. 07/08/2011)
I want this to be the start date so it would be week 3 now from this date. This is the code i have so far but just shows the ISO version:
$date = strtotime("".$row['start_date']."");
$weekNumber = date("W", $date);
print $weekNumber;
I have googled for past two hours but cannot seem to find any thing that resolves this! any help would be great thanks!
Get the difference between now and the startdate, and then divide by seven days (7*86400 seconds)
<?php
$startdate = strtotime("".$row['start_date']."");
$enddate = time();
$time_passed = $enddate - $startdate;
// if the first day after startdate is in "Week 1" according to your count
$weekcount_1 = ceil ( $time_passed / (86400*7));
// if the first day after startdate is in "Week 0" according to your count
$weekcount_0 = floor ( $time_passed / (86400*7));
?>
You can drag week number from db directly along with main date.
For example,
"SELECT start_date, (WEEK(NOW()) - WEEK(start_date)) as desired_week as week from table";

PHP Calculating future date by adding days to a variable date

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"));

Categories