php find week number from specific date - php

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

Related

get Number of month from a date php

Suppose I have a date specific as 2016-11-14 .
Also I have another date (starting) 02-14-2017 and ending 02-30-2017
I want to count the months that has passed is the date 2016-11-14 until the set of Range date.
I tried searching but I cant find a link on how to find months that has passed from a date to set of range date
DateTime() makes doing date math easy:
$datetime1 = new DateTime('2016-12-01');
$datetime2 = new DateTime('2017-02-01');
$interval = $datetime2->diff($datetime1);
echo (($interval->format('%y') * 12) + $interval->format('%m'));
Demo

Next delivery date listed on website

I'm building a website for a business that makes deliveries every second Thursday. I need to display when the next delivery is going to be, and have that date change to two weeks forward when the previous delivery date is reached.
Based on what I've been able to research so far, I've cobbled together this code:
$start_date = '2016-10-27'; // next delivery date to start counting from
// create a DateTime object that represents start of sequence
$start_datetime = DateTime::createFromFormat('Y-m-d', $start_date);
// create a DateTime object representing the current date
$current_datetime = new DateTime('today');
$date_interval = new DateInterval('P2W'); // for delivery every 2 weeks
// determine end date for DatePeriod object that will later be used
// this is no further out than current date plus the interval
$end_datetime = new DateTime('tomorrow');
$end_datetime->add($date_interval);
$date_period = new DatePeriod($start_datetime, $date_interval, $end_datetime);
// iterate until the last date in the set
foreach($date_period as $dp) {
$next_delivery = $dp;
}
?>
<div class="header-next-delivery">
Next delivery: <?php echo $next_delivery->format('l, M j, Y'); ?>
</div>
This seems to work, but I can't help thinking that there must be a more elegant way to do this than having to iterate through a set of dates from the start date to the last date in the set. As time passes, the set will just get bigger and bigger.
Also, I'm having trouble figuring out the internal workings of these functions -- how would I set the exact time that the displayed delivery date bumps forward by two weeks?
Thanks for any insight!
You can get date interval in days from $start_date to current date. Divide it by 14 (two weeks), get remainder and substract it from 14. Then you can add that value of days to current date.
$start_date = date_create('2016-10-27');
$current_date = date_create();
$interval = date_diff($start_date, $current_date);
$days_diff = (int)$interval->format('%a');
$current_date->add(14 - ($days_diff % 14).' days');

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

php MySQL Get Missing day from a Month or BETWEEN days

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

Using the PHP date and time function to print a certain sentence

I'm in need of help with the PHP date/time function.
Using de date/time function, I need to print the following:
Today is 'current day', the 'number of day of month', in the year 'year'. Today is 'number of day of the year'. We still have 'number of days till end of year'.
Just can't get this working.. any ideas?
Thanks!
Alright so there may be a better way to do this, but this is one way of getting all the different elements you asked for:
$d = new DateTime();
$currentDay = $d->format('l'); // Will give you the current day in full (i.e. Monday)
$dayOfMonth = $d->format('j'); // Will give you the current day of the Month
$year = $d->format('Y'); // Will give you the current Year.
$dayOfYear = ($d->format('z') + 1); // Starts at 0, so we add one to get the actual days past.
As for the days left, I reckon there is potentially a better way to do this, but here's a quick solution:
$daysInYear = 365;
if ($d->format('L') === 1) { // Returns 1 if leap year.
$daysInYear = 366;
}
$daysLeft = $daysInYear - $dayOfYear;
That should give you all the variables you need to print out the statement you asked for.

Categories