we have this company where they work on a yearly period of april"current year" to march"next year"
Im writing and excel report to pull out reports based on that period. where Jan, Feb, and march would be the next year , but I keep on getting the current year do you get me?
bus_year = is the year inform Jan to Dec, but its actually April to march
and year is year
I would like to know how, I could write a good logic whereby on selected months the logic would plus 1 year to the current, from instead of Jan 2013 to Jan 2014. I'm trying to brige years , and could not use unix_timestamp solely because Its just been implemented noe in the database and the is more than 5 year of data in it.
current code
private $month_array = array(
'01' => "January",
'02' => "February",
'03' => "March",
'04' => "April",
'05' => "May",
'06' => "June",
'07' => "July",
'08' => "August",
'09' => "September",
'10' => "October",
'11' => "November",
'12' => "December"
);
private $financial_year_month = array(
'04' => '01',
'05' => '02',
'06' => '03',
'07' => '04',
'08' => '05',
'09' => '06',
'10' => '07',
'11' => '08',
'12' => '09',
'01' => '10',
'02' => '11',
'03' => '12'
);
maybe to right in in a form like this for example
04-$year, 05-year, ... , 02-($year+1), 03-($year+1)
//sql query
public function get($year)
{
$select = $this->select();
$select->where('bus_year = ?', $year);
return $this->fetchAll($select);
}
Database columns Id/month/year/bus_year
Not sure how I can define and initialize year at the moment. Something like this but my current code wont take it, don't know what Im missing
if($month >= 01 && $month <= 03){ $year = $year + 1;
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 currently am working with a database that has a month column and a year column.. Both are of type text. Month is stored as 'January' and year is stored as you would expect, '2016'..
Any recommendations for concatenating these and converting them to a date type?
You can use a query like this. you only must change the strings to your fieldnames:
sample
select STR_TO_DATE(concat('2016',' ','April','1'), '%Y %M %D');
result
2016-04-01
$months = [
'january' => 1,
'february' => 2,
'march' => 3,
'april' => 4,
'may' => 5,
'june' => 6,
'july' => 7,
'august' => 8,
'september' => 9,
'october' =>10,
'november' =>11,
'december' =>12
];
$year = 2016;
$month_text = 'january';
$day = 1;
if($months[strtolower($month_text)]<10){
$month = '0'.$months[strtolower($month_text)];
}else{
$month = $months[strtolower($month_text)];
}
if($day<10){
$day = '0'.$day;
}else{
$day = $day;
}
echo $year.'-'.$month.'-'.$day;
create a new feld in db with type date and insert it into the db. maybe wrap this in a function and put it in a while loop..?
This works for me:
select convert(date,(concat('January',' ','2016')))
I've a table as following-
Now I need to make report of total number of counts in every hour, week, month and year. It may have sum 0 but should be include on the result. For example I need the result as follows-
$hourlyResult = array(
'00:01' => '5',
'00:02' => '9',
'00:03' => '50',
'00:04' => '5',
..............
..............
'00:55' => '95',
'00:56' => '0',
'00:57' => '20',
'00:58' => '33',
'00:59' => '5',
);
$weeklyResult = array(
'SAT' => '500',
'SUN' => '300'
.............
.............
'FRI' => '700'
);
How can I build the query in cakephp 3? I got the following link but can't go so far.
GROUP BY WEEK with SQL
What I've done-
$this->loadModel('Searches');
$searches = $this->Searches
->find('all')
->select(['created', 'count'])
->where('DATE(Searches.created) = DATE_SUB(CURDATE(), INTERVAL 1 DAY)')
->group(WEEK(date))
->hydrate(false)
->toArray();
pr($searches);
Here is how you can do it.
Sum By Year
$query = $this->Searches->find();
$query = $this->Searches
->find()
->select([
'total_count' => $query->func()->sum('count'),
'year' => $query->func()->year(['created' => 'literal'])
])
->group(['year'])
->hydrate(false);
Or
$query = $this->Searches
->find()
->select(['total_count' => 'SUM(count)', 'year' => 'YEAR(created)'])
->group(['year'])
->hydrate(false);
Sum By Day Of Week
$query = $this->Searches->find();
$query = $this->Searches
->find()
->select([
'total_count' => $query->func()->sum('count'),
'day_of_week' => $query->func()->dayOfWeek('created')
])
->group(['day_of_week'])
->hydrate(false);
Or
$query = $this->Searches
->find()
->select(['total_count' => 'SUM(count)', 'day_of_week' => 'DAYOFWEEK(created)'])
->group(['day_of_week'])
->hydrate(false);
The same way you can get total sum by hour or month.
Here you can read about CakePHP > Using SQL Functions and date and time functions in MySQL.
I normally use data in this format "27/04/2014 00:40" with this code below:
$f['date'] = $_POST['data'];
<form method="post" enctype="multipart/form-data">
<label class="line">
<span class="data">Date:</span>
<input type="text" id="date" name="data" value="
<?php
if(isset($_POST['date']))
echo $f['date'];
else echo date('d/m/Y H:i:s'); ?>"
/>
</label>
</form>
But now Im trying to find how I can create a format like: Sunday, 27 April, 2014
Do you know how we can do this?
You want to first parse the date with $date = date_create_from_format('d/m/Y H:i:s', $f['date']) and then format according to your new requirement: date_format('l, d F, Y', $date).
Try this:
echo date('l, j F, Y');
Read date function php manual: http://www.php.net/manual/en/function.date.php
If you need non-english names of months and names of each day you can add it by yourself.
Array for month names:
$translatedMonths = array(
'01' => 'January',
'02' => 'February',
'03' => 'March',
'04' => 'April',
'05' => 'May',
'06' => 'June',
'07' => 'July',
'08' => 'August',
'09' => 'September',
'10' => 'October',
'11' => 'November',
'02' => 'December'
);
Array for week days:
$translatedWeeks = array(
'0' => 'Sunday',
'1' => 'Monday',
'2' => 'Tuesday',
'3' => 'Thirsday',
'4' => 'Wednesday',
'5' => 'Friday',
'6' => 'Saturday'
);
Now you just need to build output string:
echo $translatedWeeks[date('w')] . ", " . date('j') . " " . $translatedMonths[date('m')] . " " . date('Y');
To use this, you just need to translate names of months and days of weeks.
Example here: http://ideone.com/BserJo
Note, that date() function returns string, so you have to build assoc arrays appropriately.
Hope, this will help you.
echo date("m", strtotime("january"));
Returns 01 as expected
echo date("m", strtotime("february"));
But this returns 03
Anyone else encountered this problem?
PHP Version 5.1.6
Today is the 29th. There is no 29th in February this year and because you're not specifying a day in February, it's using "today". The strtotime function uses relative dates so the 29th of February is basically the 1st March this year.
To solve your problem:
echo date("m", strtotime("February 1"));
As strtotime() only handles English date formats, you should maybe try using this function that I just made for you. With this you can handle month names in other languages too.
Don't know if this is essential to your application, but now you have it.
function getMonth($month, $leadingZero = true) {
$month = strtolower(trim($month)); // Normalize
$months = array('january' => '1',
'february' => '2',
'march' => '3',
'april' => '4',
'may' => '5',
'june' => '6',
'july' => '7',
'august' => '8',
'september' => '9',
'october' => '10',
'november' => '11',
'december' => '12',
'dezember' => '12', // German abrevation
'marts' => '3', // Danish abrevation for March
);
if(isset($months[$month])) {
return $leadingZero ? substr('0' . $months[$month], -2) : $months[$month];
} else {
return false;
}
}