I'm trying to create a menu that inserts the current year and archives the previous years dynamically. I have the below nested within a <ul id="Year_Menu">
$current_year = date("Y");
$nxt_yr = date("Y", strtotime('+1 years'));
if ($current_year >= $nxt_yr) {
$recent_past_year = $current_year -1;
$years = array($current_year, $recent_past_year, "2018", "2017", "2016");
} else {
$years = array($current_year, "2018", "2017", "2016");
}
foreach ($years as $value) {
echo "<li><a href='blog_posts.php?month=01&yr=$value;'>$value</a></li>";
}
</ul>
This works for this year and the next, but in 2021 the jig is up :) I can keep creating elseif statements & go up to 2030 (or whatever) but talk abt inefficient. I'm not already thinking this is the most efficient btw, but still learning, I'm trying to figure out a better way to do this, so that $recent_past_year ends up archiving, and the rest of the code just keeps moving forward.
How can I get $recent_past_year in 2021 (& beyond) to dynamically archive within the $years array? ...stuck
You might use a range to get the years using the current year as the start:
$current_year = date("Y");
$years = range(intval($current_year), 2016);
echo "<ul>";
foreach ($years as $value) {
echo "<li><a href='blog_posts.php?month=01&yr=$value;'>$value</a></li>";
}
echo "</ul>";
Result
2019201820172016
Php demo
Use PHP function range.
Example:
<?php
$thisYear = 2019;
$yearsFrom2016 = array_reverse(range(2016, $thisYear));
Related
I really need help to display a month list view in which deposit received month should be green in color and no-deposit month should be in red color.
I have stuck in comparing the array values. and I am using codeigniter.
I have a table with start-date and end-date by using this I have created an array to display all months in between these dates. Please find the code which I have used to do it below:
$datefrom = strtotime($showrangecalendar['chitdate_start']);
$dateto = strtotime($showrangecalendar['chitdate_end']);
$start_date = date('Y-m-d', $datefrom);
$end_date = date('Y-m-d', $dateto);
$day = 2.628e+6; // Day in Months
$format = 'Y-F'; // Output format (see PHP date funciton)
$sTime = strtotime($start_date); // Start as time
$eTime = strtotime($end_date); // End as time
$numDays = round(($eTime - $sTime) / $day) + 1;
$days = array();
for ($d = 0; $d < $numDays; $d++) {
$days[] = date($format, ($sTime + ($d * $day)));
}
This code got me the Days list and I have displayed this using foreach statement to a table.
Now I have another table (accounts) with columns such as ID, Book_number, Deposit_Amount and Deposit_Month. Here the problem starts I have fetched data from "accounts" and loaded to my view using the below code from controller
$data['show_account']= $this->Accounts_model->get_all_accounts($sess_data);
My Model (Accounts_Model) is as shown below:
public function get_all_accounts($id)
{
$this->db->select("*");
$this->db->order_by('book_id','desc');
$this->db->from("accounts");
$this->db->where('bh_id',$id);
$query = $this->db->get();
return $query->result();
}
If I run the belowcode:
foreach($show_account as $values){
echo $values->deposit_month;
}
Its getting me the array result of all deposit month. Suppose I have data as 2018-Sep and 2018-Oct, These 2 months column should turn green in the above mentioned $days array.
Hope I have explained my requirement clearly. Please help me with this as I am already spent long hours in this.
Thanks in Advance.
Updated:
Now could you please check my model as follows:
public function get_dep_month($bh_id)
{
$this->db->select('deposit_month');
$this->db->from('accounts');
$this->db->where('bh_id',$_SESSION['bh_id']);
$this->db->order_by('deposit_month','asc');
$query = $this->db->get();
return $query->result_array();
}
And My Controller is as follows:
$sess_data = $this->session->userdata('bh_id');
$data['depM_array'] = $this->Accounts_model->get_dep_month($sess_data);
Now please check my View as follows:
<?php
// option 2
foreach($days as $day) { // using your already exist $day for-loop for display
if (!in_array($day, $depM_array)){
$calhilight = "calgreen";
}
else{
$calhilight = "calred";
}
?>
<li class="<?php echo $calhilight; ?>"><?= $day ?></li>
<?php
}
?>
But as my doposit_month column having only 2 values ie: 2018-Sep and 2018-Oct, Instead of getting green color for those 2 values, I am getting Green for who li element. Not getting where I have done wrong.
This is the Current page view which I am getting, Actually I am expecting a calendar view with 2 green fields for 2018-Sep and 2018-Oct and all other fields in Red
Performed Var Dumb in arrays:
Pls check this screenshot
FYI:
Below is the code from where I am getting $days array.
<?php
$datefrom = strtotime($showrangecalendar['chitdate_start']);
$dateto = strtotime($showrangecalendar['chitdate_end']);
$start_date = date('Y-m-d', $datefrom);
$end_date = date('Y-m-d', $dateto);
$day = 2.628e+6; // Day in Months
$format = 'Y-F'; // Output format (see PHP date funciton)
$sTime = strtotime($start_date); // Start as time
$eTime = strtotime($end_date); // End as time
$numDays = round(($eTime - $sTime) / $day) + 1;
$days = array();
for ($d = 0; $d < $numDays; $d++) {
$days[] = date($format, ($sTime + ($d * $day)));
}
?>
Here $start_date & $end_date is fetching from another table named chitdate.
Thanks Again.
You can use array-diff or simple for-loop.
Consider the following simple example:
$days = array("2018-September", "2018-October", "2018-November", "2018-December");
$deposit_month = array("2018-September", "2018-October");
// option 1:
$diff = array_diff($days, $deposit_month);
// now $diff has: "2018-November" and "2018-December"
// option 2
foreach($days as $day) { // using your already exist $day for-loop for display
if (!in_array($day, $deposit_month))
// color in red as not found in "deposit_month"
else
// color in green
}
Nicer writing for that if can be as:
$color = in_array($day, $deposit_month) ? "Green" : "Red";
Good evening!
Problem:
The problem is to compare 2 dates in PHP.
I want to only compare day and month, excluding the year.
I want the code to first check the month, if the month is same or less than current month. If it's true, move on to check the day. If the day is equal to, or less than current day, execute a custom code.
What I've tried:
Here is where I got so far -
<?php
$oldDate = "26/02/1815";
$latestDate = explode("/", $oldDate);
$year = $latestDate[2];
$month = $latestDate[1];
$day = $latestDate[0];
$newDate = $month.'/'.$day.'/'.$year;
$nowDate = date('m/d/Y');
$nownowDate = explode("/", $nowDate);
$nowYear = $nownowDate[2];
$nowMonth = $nownowDate[0];
$nowDay = $nownowDate[1];
if ($nowMonth <= $month) {
if ($nowDay <= $day) {
echo<<<NEXTDATE
<li class="next"><?php echo link_to_next_item_show(); ?></li> //This is the custom code
NEXTDATE;
}
}
?>
I feel that there is something wrong with my IFs statement.
From:
Elegant way to get the count of months between two dates?
$timezone = new DateTimeZone('America/New_York');
$d1 = new DateTime("1815-02-26", $timezone);
$d2 = new DateTime("2015-01-01", $timezone);
var_dump($d1->diff($d2)->m); // int(4)
var_dump($d1->diff($d2)->d); // int(4)
if(($d1->diff($d2)->m) && ($d1->diff($d2)->d)){
echo "run code here";
}
You cannot put <?php tags inside a heredoc. So, instead of using a heredoc you could do:
echo '<li class="next">' . link_to_next_item_show() . '</li>';
Thank you all for trying your best at solving this problem. Maybe I didn't make it clear, that's why some of you were struggling, trying to understand what I was talking about.
I've tried to solve this problem using switch, so here is the answer that works for me.
<?php
$oldDate = metadata('item', array('Dublin Core', 'Date'));
$latestDate = explode("/", $oldDate);
$year = $latestDate[2];
$month = $latestDate[1];
$day = $latestDate[0];
$newDate = $month.'/'.$day.'/'.$year;
$nowDate = date('m/d/Y');
$nownowDate = explode("/", $nowDate);
$nowYear = $nownowDate[2];
$nowMonth = $nownowDate[0];
$nowDay = $nownowDate[1];
switch (true):
case ($month == $nowMonth):
if ($day < $nowDay) {
echo '<li class="next">' . link_to_next_item_show() . '</li>';
} else {
echo " ";
}
break;
case ($month < $nowMonth):
echo '<li class="next">' . link_to_next_item_show() . '</li>';
break;
case ($month > $nowMonth):
echo " ";
break;
default :
echo " ";
break;
endswitch;
?>
Thank you to #developerwjk for the correction on using php tag inside heredoc. Now I know what's wrong with the code. I'm providing this as an answer so that other people will benefit from this, if they are trying to compare between two dates (comparing between day and month only, regardless of its year). Hope this is useful for others in the future.
I have a database with some months. I am using the following to group similar months:
$Mydates = array();
while(........)
{
$date = $row['date'];
$Mydates[$date] = (isset($Mydates[$date])) ? $Mydates[$date] + 1: 1;
}
My table contains 4 data, 3 Jan's and 1 Feb. Now this information is being run through an array which contains the months of the year. Here is what I have so far:
$months = array("Jan","Feb","Mar",etc........);
foreach($months as $month)
{
foreach($Mydates as $Mydate => $Number)
{
if($month == $Mydate)
{
echo $month." (".$Number.")<br />";
}else{
echo $month."<br />";
}
}
}
Now when there were only Jan's this works fine. Now that I have added another month my results display like below:
Jan(3)
Jan(3)
Feb(1)
Feb(1)
Mar
Mar
etc.....
etc.....
How can I get this to only display once like this:
Jan(3)
Feb(1)
Mar
etc....
Now I have noticed that the more months add the more it duplicates. Can someone look at this and tell me where I am going wrong here?
It seems, $Mydates is already aggregated and accessible by month. Remove the inner loop and just look, if there's a corresponding month in $Mydates
foreach($months as $month) {
if(isset($Mydates[$month])) {
echo $month." (".$Mydates[$month].")<br />";
} else {
echo $month."<br />";
}
}
In the following php code, I am trying to show only those products from products.xml file that are stored in the last month. But my code does not work. Please help me in getting correct output. I also need to show products that are stored in the last 24 hours and last week.
$current_month = date("m");
$last_month = date('m', strtotime(date('-m')." -1 month"));
$xml = simplexml_load_file("products.xml");
$products = array();
foreach ($xml->product as $product) {
if ($product->date('m') == $last_month) {
$products[] = array( 'name' => (string)$product->name,
'details' => (string)$video->details );
}
}
Strtotime is nicer than you might think. For instance, this code;
if (date('m') > date('m', strtotime('last month')))
echo true;
else
echo false;
Will currently output "true" - because last month is 09, and this month is 10.
But what if this month is 01, and last month is 12? Then it would be false. I'd recommend you compare on years as well.
Edit: If this isn't a problem, and you only want to evaluate this for the most recent additions, then that should be fine. But you'll run into problems the year after if you only compare from last month, unless your import data only contains stuff from this year.
try
$last_month = date('m', strtotime("-1 month"));
$xml = simplexml_load_file("products.xml");
$products = array();
foreach ($xml->product as $product)
{
if ($product->date('m') == $last_month)
{
$products[] = array(
'name'=>(string)$product->name,
'details'=>(string)$video->details,
);
}
}
How do you create a select with the option of 2 days ahead from today picked as the default option (i.e. a 48 hour window) in PHP? This is the code I'm using so far but it doesn't work unfortunately!
<?php
$weekday = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
$days = range (1, 31);
$currentDay = date('F');
echo "<select name='weekday'>";
foreach ($days as $value) {
$default = ($value == $currentDay)?'selected="selected"':'';
echo '<option '.$default.' value="'.$value.'">'.$value."</option>\n";
}
echo '</select> ';
?>
I'm confused as to what your code does.
As far as I can tell $weekday is not used after being instantiated, and you are setting $currentDay to the text representation of the current month (e.g. September).
But if you want to get the day of month of the day 48 hours from now:
$two_days_ahead = date('j', strtotime('+ 48 hours'));