I have a table with sales. From this table i take all results of the last 30 days, sum the prices with the same date and get this as array.
SQL:
SELECT date
, price
, id
, SUM(price) AS daylieprice
FROM sales
WHERE id = :id
AND date BETWEEN DATE_FORMAT(CURDATE() , '%Y-%m-%d') - interval 1 month AND DATE_FORMAT(CURDATE() , '%Y-%m-%d'))
GROUP
BY date
So i have for example:
ARRAY ['date'] - ARRAY ['daylieprice']
"2017-03-29" - "1"
"2017-04-02" - "5"
"2017-04-04" - "3"
Google chart is looking like that:
['<? echo date('d', strtotime("-2 day")) ?>', VALUE]
['<? echo date('d', strtotime("-1 day")) ?>', VALUE]
['<? echo date('d') ?> ', VALUE]
Is there a way to output the value of the array like that:
date('d', strtotime("-2 day") , ARRAY ['daylieprice']);
date('d', strtotime("-1 day") , ARRAY ['daylieprice']);
date('d', ARRAY ['daylieprice']);
Should mean to take the array value easy with date('d') or date('d', strtotime("-1 day") witouth making a loop for each value ?
Or does i have to make for every day a sql request?
I came up with this. I use DateTime to give more control and felxibility with input and output formats. This loops through your input array and subtracts 2 days from first entry, 1 day from 2nd entry and keeps 3rd entry the same:
<?php
$input = [
[
'date' => '2017-03-29',
'daylieprice' => 1,
],
[
'date' => '2017-04-02',
'daylieprice' => 5,
],
[
'date' => '2017-04-04',
'daylieprice' => 3,
],
];
$output = [];
$number_of_dates = count($input) - 1;
foreach ($input as $v) {
$date = DateTime::createFromFormat('Y-m-d', $v['date'])
->modify(sprintf('-%d days', $number_of_dates))
->format('Y-m-d');
$number_of_dates--;
$output[] = "'" . $date . "', " . $v['daylieprice'];
}
This produces an array like:
Array
(
[0] => '2017-03-27', 1
[1] => '2017-04-01', 5
[2] => '2017-04-04', 3
)
Hope this helps and you can figure out exactly how to implement it to solve your problem.
Edit: just saw echo date('d' so maybe you only want the day of the month, that's easy, you can just change ->format('Y-m-d'); in the loop to ->format('d');
Demo: https://eval.in/784353
Related
I want to display the start date and end date of the week. I have One date and a string like 1W4 and,in 1W4 consider 4 weeks and 1 visit so, my string like this 2W4,1W2,3W3,1W1,2W4.
I want to make start date and end date of week array according to string and week start from Sunday to Saturday.
Please post me if anyone has solution.Please ignoring if mistake in asking Question.
Thank you.
Try my php code:
From php.net datetime.format:
W: ISO-8601 week number of year, weeks starting on Monday.
The first calendar week of a year is that one which includes the first Thursday of that year.
So I have to rest one day to the start week date.
I assumed that the weeks correspond to the current year.
Input string:
$weeksString = "2W4,1W2,3W3,1W1,2W4";
Code:
<?php
$weeksArray = explode(",", $weeksString);
$result = array();
foreach($weeksArray as $visitsWeek) {
list($visits, $week) = explode("W", $visitsWeek);
$startDate = date("Y-m-d", strtotime(date("Y") . "W" . str_pad($week, 2, "0", STR_PAD_LEFT) . " -1 days"));
$endDate = date("Y-m-d", strtotime($startDate . " +6 days"));
$result[] = array("week" => $week, "startDate" => $startDate, "endDate" => $endDate);
}
?>
Output array $result:
array ( 0 => array ( 'week' => '4', 'startDate' => '2021-01-24', 'endDate' => '2021-01-30', ), 1 => array ( 'week' => '2', 'startDate' => '2021-01-10', 'endDate' => '2021-01-16', ), 2 => array ( 'week' => '3', 'startDate' => '2021-01-17', 'endDate' => '2021-01-23', ), 3 => array ( 'week' => '1', 'startDate' => '2021-01-03', 'endDate' => '2021-01-09', ), 4 => array ( 'week' => '4', 'startDate' => '2021-01-24', 'endDate' => '2021-01-30', ), )
I need to loop dates, according to different $eventname. I was already able to write a script that adds one week to the original date, but I don't know how I can loop it for a defined time.
code used:
$eventname = $event->title;
// TODO: loop for specified times if $eventname contains definded strings
$start_date = helper('com://site/ohanah.date.format', array(
'date' => $event->start,
'format' => 'Y/m/d H:i',
'timezone' => 'UTC'
));
$date = strtotime($start_date) + 604800;
echo "<pre>";
echo date('d. F Y, H:i', $date);
echo ' - ';
echo helper('com://site/ohanah.date.format', array(
'date' => $event->end,
'format' => 'H:i',
'timezone' => 'UTC'
));
echo "</pre>";
Output: (start date would be one week before) 18. April 2018, 14:00 - 16:00
So my question is, how can I loop this that the output is e.g. 6 times with one week space between each of them?
When working with dates and times, do not add seconds to timestamps or something like that, because it will get you in trouble in leapyears and daylight saving times, because one day is not always 86400 seconds.
Better use PHP's DateTime and DateInterval classes.
<?php
$Date = new DateTime("2018-03-03 14:00:00");
for($i=0;$i<6;$i++) { //loop 6 times
$Date->add(new DateInterval('P1W')); //add one week
echo $Date->format("Y-m-d H:i:s").PHP_EOL;
}
Output:
2018-03-10 14:00:00
2018-03-17 14:00:00
2018-03-24 14:00:00
2018-03-31 14:00:00
2018-04-07 14:00:00
See also:
http://php.net/manual/en/class.datetime.php
http://php.net/manual/en/class.dateinterval.php
Something like that?
<?php
$oneWeek = 604800;
$date = '2018-04-05';
$dates = array($date);
for ($i = 0; $i < 6; $i++) {
$dates[] = $date = date('Y-m-d', strtotime($date) + $oneWeek);
}
var_dump($dates);
I am not entirely sure I understand the question, but it looks like you want to have a condition that will either set a specific number of times for the output loop or determine whether the loop is ran that number of times.
If so, you can set a counter variable with your condition, then run the loop that number of times, defaulting the counter variable to 1 in the case that you do not want to output more than one time:
$eventname = $event->title;
// TODO: loop for specified times if $eventname contains definded strings
$counter = (/*your condition for $eventname*/) ? 6 : 1;
for ($x = 0; $x < $counter; $x++) {
$start_date = helper('com://site/ohanah.date.format', array(
'date' => $event->start,
'format' => 'Y/m/d H:i',
'timezone' => 'UTC'
));
$date = strtotime($start_date) + 604800;
echo "<pre>";
echo date('d. F Y, H:i', $date);
echo ' - ';
echo helper('com://site/ohanah.date.format', array(
'date' => $event->end,
'format' => 'H:i',
'timezone' => 'UTC'
));
echo "</pre>";
}
I posted a question some time ago on representing data per date horizontally on a datatable.
See here: datatables dates at the top and data cells going from left to right
With the help of that thread I was able to get the data to display how I wanted it. With the dates showing at the top, the service provided on the left and all data associated with any date between the 2 date paramters inside the main body. (If there is no data in a particular date then the < td > will display 0. See here:
http://www.phpwin.org/s/ewbAS6
After manipulating this code further I made the dates of the search dynamic by proving a form with a start date and an end date, and a dropdown with the options of:
Daily
Weekly
Monthly
Quaterly
Yearly
this allows the interval of dates at the top to become dynamic. Of course all this is doing is changing the value of the 2nd parameter inside the date while loop.
WHILE (strtotime($date) <= strtotime($end_date)) {
echo '<th>' . $date . '</th>';
$date = date('Y-m-d', strtotime($date . ' +1day'));
}
with the parameter set at Weekly, the value of +1day becomes +1week, at Monthly; the value becomes +1month and so on.
MY ISSUE:
When the interval is set to daily, the dates with their corresponding attendance counts are displayed correctly but once you try to increase the interval to +1week and above the data does not round up to the week shown. Check this:
[LINK1]Per day: http://www.phpwin.org/s/ewbAS6
[LINK2]Per month: http://www.phpwin.org/s/xRo3I6
Looking at the array (modified on the LINK2)
$result[] = array('Service Name' => 'Health', 'date' => '2017-04-04', 'Attendance' => 5);
$result[] = array('Service Name' => 'Payroll', 'date' => '2017-04-16', 'Attendance' => 5);
$result[] = array('Service Name' => 'Saturday Youth Meeting', 'date' => '2017-04-03', 'Attendance' => 1);
$result[] = array('Service Name' => 'Saturday Youth Meeting', 'date' => '2017-05-03', 'Attendance' => 3);
$result[] = array('Service Name' => 'Payroll', 'date' => '2017-05-03', 'Attendance' => 2);
$result[] = array('Service Name' => 'Payroll', 'date' => '2017-04-11', 'Attendance' => 3);
$result[] = array('Service Name' => 'Payroll', 'date' => '2018-04-03', 'Attendance' => 10);
You can see in the array that there are multiple attendance entries in April, totaling 14 Attendances in that month however during LINK2 where the interval is increased to a month instead of showing 14 for April (which would be the sum of all the dates in that particular month) it shows the value 1.
My live version takes the array from a database so I used the YEAR(), MONTH(), WEEK() and DAY() function on the date and used group by. The query executes how I want it but having issues working on the PHP end.
I am trying to check if a post stored in a database is older then 1, 2, 3, and, finally, 4 days, respectively.
The table storing all of the posts has a date field. I have a query that retrieves the date and then I try to check if the date is older than 1, 2, 3 and 4 days, respectively and based on the result I want to move posts around the page.
I have the following:
foreach($this->getArticleData() as $i)
{
if(strtotime($i['date']) > strtotime('-1 day'))
{
$this->priority = '0.9';
}
elseif(strtotime($i['date']) > strtotime('-2 day'))
{
$this->priority = '0.8';
}
elseif(strtotime($i['date']) > strtotime('-3 day'))
{
$this->priority = '0.7';
}
else(strtotime($i['date']) > strtotime('-4 day'))
{
$this->priority = '0.6';
}
}
I do not think that code is working properly. In some instances the priorities are wrong. I am I using srttotime() function is is there another more reliable way to do this?
Forward slash (/) signifies American M/D/Y formatting, a dash (-) signifies European D-M-Y and a period (.) signifies ISO Y.M.D.
Your database uses the -'s, you're sure that's right? Anyway, a more sufficient way of your code would be:
foreach ( $this->getArticleData() as $i ) {
for ( $x = 1; $x < 10; $x++ ) {
if ( strtotime ( $i [ 'date' ] ) > strtotime ( '-'. $x ." day" ) ) {
$this->priority = '0.'. 10 - $i;
}
}
}
Flip your > around. You're checking if the date is greater/newer than the specified time. You'll also need to flip your if statement, because you're checking newest to oldest. A post that is 4 days old is also 1 day old, and will be caught by the first block.
I would prefer using a priority map and DateTime :
$list = array(
array( 'date' => '2014-12-14' ) ,
array( 'date' => '2014-12-15' ) ,
array( 'date' => '2014-12-14' ) ,
array( 'date' => '2014-12-11' ) ,
array( 'date' => '2014-12-14' ) ,
array( 'date' => '2014-12-13' ) ,
);
# maps days to priority
$priorities = array(
1 => 0.9,
2 => 0.8,
3 => 0.7,
4 => 0.6
);
$currentDate = new DateTime();
foreach( $list as $i ) {
$dateTime = new DateTime( $i['date'] );
$diff = $currentDate->diff( $dateTime );
$days = $diff->format( '%d' );
if( isset( $priorities[ $days ] ) ){
echo 'Date is: ' . $i['date'] . "| Difference is : " . $days . "| Priority is: " . $priorities[ $days ]. "<br/>";
}
}
Result:
Date is: 2014-12-14| Difference is : 1| Priority is: 0.9
Date is: 2014-12-14| Difference is : 1| Priority is: 0.9
Date is: 2014-12-11| Difference is : 4| Priority is: 0.6
Date is: 2014-12-14| Difference is : 1| Priority is: 0.9
Date is: 2014-12-13| Difference is : 2| Priority is: 0.8
I am building a small class combination to calculate the precise date of the beginning of a semester. The rules for determining the beginning of the semester goes as follow :
The monday of week number ## and after dd-mm-yyyy date
ie: for winter its week number 2 and it must be after the january 8th of that year
I am building a resource class that contain these data for all the semesters (4 in total). But now I am facing an issue based on the public holidays. Since some of those might be on a Monday, in those cases I need to get the date of the Tuesday.
The issue I am currently working on is the following :
The target semester begins on or after august 30 and must be on week 35.
I also have to take account of a public holiday which happen on the first monday of september.
The condition in PHP terms is the following
if (date('m', myDate) == 9 // if the month is september
&& date('w', myDate) == 1 // if the day of the week is monday
&& date('d', myDate) < 7 // if we are in the first 7 days of september
)
What would be the best way to "word" this as a condition and store it in an array?
EDIT
I might not have been clear enough, finding the date is not the problem here. The actual problem is storing a condition in a configuration array that looks like the following :
$_ressources = array(
1 => array(
'dateMin' => '08-01-%',
'weekNumber' => 2,
'name' => 'Winter',
'conditions' => array()
),
2 => array(
'dateMin' => '30-04-%',
'weekNumber' => 18,
'name' => 'Spring',
'conditions' => array()
),
3 => array(
'dateMin' => '02-07-%',
'weekNumber' => 27,
'name' => 'Summer',
'conditions' => array()
),
4 => array(
'dateMin' => '30-08-%',
'weekNumber' => 35,
'name' => 'Autumn',
'conditions' => array("date('m', %date%) == 9 && date('w', %date%) == 1 && date('d', %date%) < 7")
)
);
The issue I have with the way it's presented now, is that I will have to use the eval() function, which I would rather not to.
You said:
The target semester begins on or after august 30 and must be on week 35.
If that's the case you can simple check for week number.
if(date('W', myDate) == 35)
Or if your testing condition is correct then you should compare day number till 7 as it starts from 1.
if((date('m', myDate) == 9 // september
&& date('w', myDate) == 1 // monday
&& date('d', myDate) <= 7 // first 7 days of september
)
And then in the if statement, once you have found the monday which would be OK IF its not a public holiday, do this
if(...){
while(!array_search (myDate, aray_of_public_holidays))
date_add($myDate, date_interval_create_from_date_string('1 days'));
}
Here the array_of_public_holidays contains the list of public holidays.
Update with Code
Following code should work for your purposes
<?php
// array with public holidays
$public_holidays = array(/* public holidays */);
// start on 30th august
$myDate = new DateTime('August 30');
// loop till week number does not cross 35
while($myDate->format('W') <= 35){
// if its a monday
if($myDate->format('w') == 1){
// find the next date not a public holiday
while(array_search($myDate, $public_holidays))
$myDate->add(date_interval_create_from_date_string('1 days'));
// now myDate stores the valid semester start date so exit loop
break;
}
// next date
$myDate->add(date_interval_create_from_date_string('1 days'));
}
// now myDate is the semester start date
?>
Update according to updated question
Following code should work for your needs. You do not need to store the condition in your array as PHP code. The following code shows how it can be done
// semester conditions
$sem_conditions = array(
1 => array(
'dateMin' => '08-01-%',
'weekNumber' => 2,
'name' => 'Winter'
),
2 => array(
'dateMin' => '30-04-%',
'weekNumber' => 18,
'name' => 'Spring'
),
3 => array(
'dateMin' => '02-07-%',
'weekNumber' => 27,
'name' => 'Summer'
),
4 => array(
'dateMin' => '30-08-%',
'weekNumber' => 35,
'name' => 'Autumn'
)
);
// array with public holidays format (d-M)
$public_holidays = array('05-09', '10-01');
// store sem starts
$sem_starts = array();
// for each semester
foreach($sem_conditions as $sem){
// start date
$myDate = date_create_from_format('d-m', substr($sem['dateMin'], 0, -2));
// loop till week number does not cross $sem['weekNumber']
while($myDate->format('W') <= $sem['weekNumber']){
// if its a monday
if($myDate->format('w') == 1){
// find the next date not a public holiday
while(array_search($myDate->format('d-m'), $public_holidays) !== false)
$myDate->add(date_interval_create_from_date_string('1 days'));
// now myDate stores the valid semester start date so exit loop
break;
}
// next date
$myDate->add(date_interval_create_from_date_string('1 days'));
}
// add to sem starts
$sem_start[$sem['name']] = $myDate->format('d-m-Y');
}
var_dump($sem_start);
The target semester begins on or after august 30 and must be on week 35
The start of the semester is the minimal date between week 35 and August 30:
$week35 = new DateTime("January 1 + 35 weeks");
$august30 = new DateTime("August 30");
$start = min($week35, $august30);
Alternatively:
$start = min(date_create("January 1 + 52 weeks"), date_create("August 30"));