PHP - Compare Current Date To Another ...
I am building a site that needs to update member accounts on a weekly basis. At the moment I have a cron job set for 12:01 am on Monday, that resets five database fields for all of my members. The first field is the current week, the second field is the previous week, the third field is from two weeks ago, the fourth field is from three weeks ago, and the fifth field is from four weeks ago. Obviously the cron job resets the first field to 0, and the data from this field is moved to field two (last week), and so on and so forth, and the data from the fifth field is just removed altogether. However, this is really not preferred, because if/when I get 1000+ users, the cron job will take up a crapload of CPU as many mysql queries need to be executed for each account.
So, I am trying to implement a script that checks the last login date for a user, and if they have have not logged in since before the latest Monday, the Monday before that, the Monday before that, or the Monday before that, etc. I have never really worked with dates, so I don't even know how to begin. The last user login is stored as a DATA datatype in a mysql database, and is in the following format: YYYY-MM-DD. Thanks in advance! Similar TutorialsHi, Currently I am making a module for joomla. every article has an publish date, if the article was published in 7 days ago, it will displayed as "article in last week", My idea is to use today's date - publish date, if the result is greater than 7 and smaller than 14, the article will be displayed as "article in last week. Any one know how to write this code? Here is what I have got, but not working. <?php $todays_date = date("Y-m-d"); $result = mysql_query("select * from jos_content where $test between $todays_date-14 and $todays_date-7"); while($row = mysql_fetch_array($result)) { echo "$todays_date - $row[title]"; } ?> I'm trying to do the following PHP. I have written it in English if (today's date) => date1 AND <= date2 then {display image1} elseif (today's date) => date3 AND <= date4 then {display image2} else {display image 13} There are 24 fixed dates and 13 fixed images. I have tried using combinations of strtotime(), replacing the date value with variables, hard coding the dates within the program. I don't seem to be able to get any combination to work properly. I'm sure it's just a syntax error but I can't see it. When I've searched the web all the answers I've found relate to dates within databases but as I only have 24 dates it seems a bit of overkill. I would appreciate any pointers to the correct method I might be able to use. The dates need only a day and month as I would like this to repeat year after year. <?php $today = strtotime(date('d-m')); if (strtotime($today) >= strtotime('28-10') && strtotime($today) <= strtotime('24-11')) {echo "<div>image1</div>";} elseif (strtotime($today) >= strtotime('25-11') && strtotime($today) <= strtotime('22-12')) {echo "<div>image2</div>" ;} elseif (strtotime($today) >= strtotime('23-12') && strtotime($today) <= strtotime('24-02')) {echo "<div>image3</div>" ;} else {echo "<div>image13</div>";} ?> The result I get from this is image1 appears on the web page but I would expect image3 as today is 22-01 Thank you in advance. Andrew Hi, I am trying to write an SQL to XML converter, so so far I have all the records inputted into the db tables, and now I want to append a child, but I don't know how to compare current row's field value with the next row below it, specifically I want to check if a current row is parent of the next row. I have already got the XML to SQL function working (some bugs but for the most part it's working) So for eg: <?php $query=mysql_query("SELECT * FROM edge"); while($row=mysql_fetch_assoc($query) ) { if($row['target'] is parent of the next row), then append the next row as child of current row } ?> Table edge is one of the tables that hold target node/source(aka: parent) nodes. Any help much appreciated! Hey, I'm using a script which allows you to click on a calendar to select the date to submit to the database. The date is submitted like this: 2014-02-08 Is there a really simple way to prevent rows showing if the date is in the past? Something like this: if($currentdate < 2014-02-08 || $currentdate == 2014-02-08) { } Thanks very much, Jack (continuing from topic title) So if I set a date of July 7 2011 into my script, hard coded in, I would like the current date to be checked against the hard coded date, and return true if the current date is within a week leading up to the hard coded date. How could I go about doing this easily? I've been researching dates in php but I can't seem to work out the best way to achieve what I'm after. Cheers Denno how to compare date with current date let say $date="2010-10-08 2:00 PM" how can i compare $date with current date and get the result in days, hours, minutes and seconds thanks I'm going crazy. I have two arrays. The first has working hours and the array is like this: ```array(7) { [0]=> array(4) { ["morning_from"]=> string(6) "closed" ["morning_to"]=> string(6) "closed" ["afternoon_from"]=> string(5) "13:00" ["afternoon_to"]=> string(5) "19:00" }``` The second one is the busy times, the array is like this: ```array(2) { [0]=> { ["title"]=> string(13) "Test" ["start_date"]=> string(19) "2019-11-25 13:00:00" ["end_date"]=> string(19) "2019-11-25 14:00:00" } }```
Now I do a cycle of 15 times because I need to make the list for the next 15 days from today.
``` Unfortunately it does not work as I would like, it does not mark correctly if the date is occupied. I'll post a piece of code:
if($book_i == 0){
$book_day = ucwords(strftime('%d %h', strtotime($book_today_day)));
if(count($getListCalendarStartToday) > $count_list_calendar_p){
$count_more_appointment_on_some_days_morning = 0;
foreach ($getListCalendarStartToday as $key => $value) {
if($day == $book_today_day){ $book_output .= "<li>"; if(isset($user["working_time"]) && $user["working_time"]){ $book_output .= "<div><p>".$book_name_day."</p><p>".$book_day."</p></div>";
if($book_name_day == $lang["days-mon"]){
$book_morning_from = $book_array_working_time[$book_day_int]["morning_from"];
$book_morning_from_hour = (int)substr($book_morning_from, 0, 2);
$book_afternoon_from_hour = (int)substr($book_afternoon_from, 0, 2);
$book_morning_from_hour_duplicated = $book_morning_from_hour;
$book_afternoon_from_hour_duplicated = $book_afternoon_from_hour;
$book_check_closed = 0;
if($book_afternoon_from_hour == 0 && $book_afternoon_to_hour == 0){ $book_output .= "<div>";
for ($n=0; $n < $count_more_appointment_on_some_days_afternoon; $n++) {
$day_appointment_p_start_hour = date('H', strtotime($getListCalendarStartToday[$count_list_calendar_p]["start_date"]));
if($book_afternoon_from_hour_duplicated != $book_afternoon_from_hour){
if($book_afternoon_from_hour != $book_afternoon_to_hour){
$book_afternoon_from_hour_duplicated += 1; if($book_afternoon_from_min == 0){
if($day_appointment_p_start_hour == $day_appointment_p_end_hour){
$count_hour_appointment = 0;
$count_hour_appointment += 1;
$book_afternoon_from_hour_duplicated += $count_hour_appointment; break; }else{ if($book_i == 0){
if($day_appointment_p_start_min >= $book_afternoon_from_min && $day_appointment_p_start_min <= 30){ }else{
if($day_appointment_p_start_min >= $book_afternoon_from_min && $day_appointment_p_start_min >= 30){ }
$book_output .= "<a class='uk-button-book-a-visit-hour' id='a-book-a-visit' data-book-day='".$book_today_day."' data-book-hour='".number_add_leading_0($book_afternoon_from_hour).":00'>".number_add_leading_0($book_afternoon_from_hour).":00</a>"; }else{
$book_afternoon_from_hour_duplicated += 1;
if($book_afternoon_from_min == 0){
if($book_afternoon_from_min == $book_afternoon_to_min){
$book_output .= "</div>";
$book_output .= "</li>"; Hi, I have database stored a field of date. I want to check if the date in the DB field is the same as the current date, how to do it? Code: $oracle = mysqli_real_escape_string($con, $_POST['oracle']); $query = mysqli_query($con, "SELECT indate FROM staff WHERE OracleID ='$oracle'"); $row = mysqli_fetch_array($query); $num_row = mysqli_num_rows($query); if ($num_row > 0) { $curdate = date(); if ($row['indate'] !== $curdate) { $query1=mysqli_query($con, "update staff set ClockedIn = 0, ClockedOut = 0 where OracleID='$session_id'")or die('Error In Session'); header('location:home.php'); } }
What I am trying to do is I am making a Event page and I would like to have it list the events in 2 sections. One being "On" or "Before" current date and the next being "Past" current date so I can list the events in "Upcoming Events" and "Past Events" the code im showing here is for the listing of my event page I just need help turning this query into two separate querys with the date function im looking for. Code: [Select] <?php $result = mysql_query("SELECT * FROM event ORDER by eventdate DESC"); if (!$result) { die("query failed: " . msql_error()); } while ($row = mysql_fetch_array($result)) { list($id, $eventdate, $header, $description, $image, $location) = $row; $description = nl2br($description); $eventdate = date("M j, Y",strtotime("$eventdate")); print(' <table width="680" border="0" cellpadding="14" cellspacing="0"> <tr> <td> <div class="myFont"><font size="+1" color="#4e8baf">'.$eventdate.' - </font><font size="+1"><b>'.$header.'</b></font></div> <font size="+1"><a href="event.php?list=true&eventid='.$id.'">Details</a></font><br /> <hr color="#FFFFFF" width="100" align="left" size="1"> </td> </tr> </table> '); } ?> Hi, when i try to insert to table date value it gives me wrong current date. $joindate = date('m-d-Y'); $sql = mysqli_query($con,"INSERT INTO staff (OracleID,StaffName,Des,joindate,username,password,isadmin) VALUES ('$oracleid','$name','$des','$joindate','$username', '$pass','$isadmin')"); i get this in the main page after making date("d-m-Y",strtotime(date)) 30-11--0001 why is that? Edited April 7, 2020 by ramiwahdanI'm looking to enter the current time and date into a mysql database using php. My current code looks like this: Code: [Select] $now = date("m/d/y",time()); I then insert $now into the table into a datetime column. This all works but the time when read from the column appears as 0000-00-00 00:00:00. Any ideas what I'm doing wrong? Thanks in advance. I'm building an article system, what im trying to do is when a user choose the article to be published tomorrow to able to. Write today the article but the system will show it from tomorrow Code: [Select] $gettoday = date("Y:m:d"); $query = "SELECT * FROM tblnews WHERE MainArticle = 1 AND NewsDate = '".$gettoday."' ORDER BY `Id` DESC LIMIT 1"; $result = mysql_query($query); This is my code, but when i use this and the article is not posted for today is not showing anything.. any suggestions please? Thank you This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=342459.0 I am writing code in the ZEND framework and i need to get the current day in like 2012-02-03: In the controller: Code: [Select] $time=date('YYYY-mm-dd',time()); $this->view->time=$time;In view: Code: [Select] <?php echo $this->time; ?> Output: 2012201220122012-0202-0303 I dont know why it is displaying like that instead of once...there is no loop anywhere. Hi everyone, I have a basic calendar that I am trying to change to highlight the current date. I have got the current date highlighted using an if else statement inside a while clause, however, the issue arises when I use the forward/previous links to go to a different month - the same day is highlighted on those months as well. Here is the code snippet in question (the entire code is below): while( $day_num <= $days_in_month ) { if(date('d') != $day_num) { echo "<td> $day_num </td>"; $day_num++; $day_count++; } // display today's date else { echo "<td class=\"today\"> $day_num </td>"; $day_num++; $day_count++; } My question is how do I get this calendar to display the current date, just on the current month. I assume there is something I am missing when it comes to handling the date(s) correctly, but what? Any help is appreciated. Thanks, kaiman Full code: // gets today's date $date = time () ; // puts the day, month, and year in seperate variables $day = date('d', $date) ; $month = (int) ($_GET['month'] ? $_GET['month'] : date('m')); $year = (int) ($_GET['year'] ? $_GET['year'] : date('Y')); // generate the first day of the month $first_day = mktime(0,0,0,$month, 1, $year) ; // get the month name $current_month = date('F', $first_day) ; // determine what day of the week the first day of the month falls on $day_of_week = date('D', $first_day) ; // determine blank days before start of month switch($day_of_week){ case "Sun": $blank = 0; break; case "Mon": $blank = 1; break; case "Tue": $blank = 2; break; case "Wed": $blank = 3; break; case "Thu": $blank = 4; break; case "Fri": $blank = 5; break; case "Sat": $blank = 6; break; } // determine how many days are in the current month $days_in_month = cal_days_in_month(0, $month, $year) ; // determine next month $next_month_link = '<a href="?month='.($month != 12 ? $month + 1 : 1).'&year='.($month != 12 ? $year : $year + 1).'">>></a>'; // determine previous month $previous_month_link = '<a href="?month='.($month != 1 ? $month - 1 : 12).'&year='.($month != 1 ? $year : $year - 1).'"><<</a>'; // start building the table echo " <table border=0 width=294>\n"; echo " <tr>\n"; // display month and year echo " <th> $previous_month_link </th>\n"; echo " <th colspan=4> $current_month $year </th>\n"; echo " <th> $next_month_link </th>\n"; echo " </tr>\n"; // display days of week echo " <tr>\n"; echo " <td width=42>S</td>\n"; echo " <td width=42>M</td>\n"; echo " <td width=42>T</td>\n"; echo " <td width=42>W</td>\n"; echo " <td width=42>T</td>\n"; echo " <td width=42>F</td>\n"; echo " <td width=42>S</td>\n"; echo " </tr>\n"; // counts the days in the week $day_count = 1; echo " <tr>\n"; // display blank days while ( $blank > 0 ) { echo " <td></td>\n"; $blank = $blank-1; $day_count++; } // sets the first day of the month to 1 $day_num = 1; // count the days in the month and display them while( $day_num <= $days_in_month ) { if(date('d') != $day_num) { echo "<td> $day_num </td>"; $day_num++; $day_count++; } // display today's date else { echo "<td class=\"today\"> $day_num </td>"; $day_num++; $day_count++; } // make sure we start a new row every week if ($day_count > 7) { echo " </tr>\n"; echo " <tr>\n"; $day_count = 1; } } // finish calendar while ( $day_count >1 && $day_count <=7 ) { echo " <td>\n"; echo " </td>\n"; $day_count++; } echo " </tr>\n"; echo " </table>\n"; Hey guys,
Ive been working on this script and I cant seem to figure out how to highlite the current date in lets say a blue color.
Any ideas please?
<!DOCTYPE html> <html> <head> <title>Simple Month Calendar</title> <script type="text/javascript"> // Author: Danny van der ven // Created: 12 aug 2014 // P.S. I'm from The Netherlands, so the names of the weeks and months are in Dutch. var Calendar = function(divId) { //Store div id this.divId = divId; // Days of week, starting on Sunday this.DaysOfWeek = [ 'Z', 'M', 'D', 'W', 'D', 'V', 'Z' ]; // Months, stating on January this.Months = ['januari', 'februari', 'maart', 'april', 'mei', 'juni', 'juli', 'augustus', 'september', 'oktober', 'november', 'december' ]; // Set the current month, year var d = new Date(); this.CurrentMonth = d.getMonth(); this.CurrentYear = d.getFullYear(); }; // Goes to next month Calendar.prototype.nextMonth = function() { if ( this.CurrentMonth == 11 ) { this.CurrentMonth = 0; this.CurrentYear = this.CurrentYear + 1; } else { this.CurrentMonth = this.CurrentMonth + 1; } this.showCurrent(); }; // Goes to previous month Calendar.prototype.previousMonth = function() { if ( this.CurrentMonth == 0 ) { this.CurrentMonth = 11; this.CurrentYear = this.CurrentYear - 1; } else { this.CurrentMonth = this.CurrentMonth - 1; } this.showCurrent(); }; // Show current month Calendar.prototype.showCurrent = function() { this.showMonth(this.CurrentYear, this.CurrentMonth); }; // Show month (year, month) Calendar.prototype.showMonth = function(y, m) { var d = new Date() // First day of the week in the selected month , firstDayOfMonth = new Date(y, m, 1).getDay() // Last day of the selected month , lastDateOfMonth = new Date(y, m+1, 0).getDate() // Last day of the previous month , lastDayOfLastMonth = m == 0 ? new Date(y-1, 11, 0).getDate() : new Date(y, m, 0).getDate(); var html = '<table>'; // Write selected month and year html += '<tr><td colspan="7">' + this.Months[m] + ' - ' + y + '</td></tr>'; // Write the header of the days of the week html += '<tr>'; for(var i=0; i < this.DaysOfWeek.length;i++) { html += '<td>' + this.DaysOfWeek[i] + '</td>'; } html += '</tr>'; // Write the days var i=1; do { var dow = new Date(y, m, i).getDay(); // If Sunday, start new row if ( dow == 0 ) { html += '<tr>'; } // If not Sunday but first day of the month // it will write the last days from the previous month else if ( i == 1 ) { html += '<tr>'; var k = lastDayOfLastMonth - firstDayOfMonth+1; for(var j=0; j < firstDayOfMonth; j++) { html += '<td class="not-current">' + k + '</td>'; k++; } } // Write the current day in the loop html += '<td>' + i + '</td>'; // If Saturday, closes the row if ( dow == 6 ) { html += '</tr>'; } // If not Saturday, but last day of the selected month // it will write the next few days from the next month else if ( i == lastDateOfMonth ) { var k=1; for(dow; dow < 6; dow++) { html += '<td class="not-current">' + k + '</td>'; k++; } } i++; }while(i <= lastDateOfMonth); // Closes table html += '</table>'; // Write HTML to the div document.getElementById(this.divId).innerHTML = html; }; // On Load of the window window.onload = function() { // Start calendar var c = new Calendar("divCalendar"); c.showCurrent(); // Bind next and previous button clicks getId('btnNext').onclick = function() { c.nextMonth(); }; getId('btnPrev').onclick = function() { c.previousMonth(); }; } // Get element by id function getId(id) { return document.getElementById(id); } </script> <style type="text/css"> td.not-current { color: #777; } </style> </head> <body> <div id="divCalendar"> </div> <button id="btnPrev" type="button">Terug</button> <button id="btnNext" type="button">Vooruit</button> </body> </html> Hi there, I've just started learning php and mysql today. I'm putting things together in dreamweaver and everything is running smoothly (ish). I've got a form written in php that sends data to a database. All fine. I need to send the time of submission to the database. I've put in a hidden field, what code should I use to set the field value to a mysql field of type DATETIME? Cheers. Hi, I have a problem. i want to show the current date and time in my tables of the database. I want to show it because i want to show it on my report generated in php. By the way i do not have a "date" field in my form. Can anyone help me out? Thanks, Heshan. I would like to know the best practice to achieve the following: I have a list of dates related to live events for performing artists. When someone views the web page that contains a section to display the dates, I would only want to show the dates from today into the future and not show any dates from the past. What is the best way to accomplish this? Thanks in advance... I have a field called date_time in the database which is of type datetime. I have a form which on submit needs to enter the current date and time in the date_time field. I know I can get the current time using time() $time=time(); Can do I do some kind of manipulation using strtotime() and store in the database in datetime format? |