PHP - Diffrence Between Two Dates
hey days im trying to get the differnce between the two dates but
Code: [Select] $start_date = new DateTime($start_date); $todays_date = new DateTime("now"); $interval = $start_date->diff($todays_date); echo $interval->format('%R%a days'); but the error im getting is Fatal error: Call to undefined method DateTime::diff() i dont see what im doing wrong if someone could help please thank you Similar TutorialsHi all. How can i see all the difference between two dates in a column. I know to see a single date difference we can do: $stmt = $pdo->query("SELECT DATEDIFF(date_paid, next_due) AS diffdate FROM table_name"); while($row = $stmt->fetch(PDO::FETCH_ASSOC)) { echo $row['diffdate']; } but i want all the difference to show in a table along with other details. thanks Hi I have two filed in database storing time like this 19:00 and other 06:00 i need to get difference between both which is 13 hours. Thanks Hi All, I need to subtract dates and display the number of days left. I have a 'Start' date and an 'End' date in DATETIME format in the DB. Not quite sure where to start. A simply start - end doesn't work . Start = 2011-11-01-00:00:00 End = 2011-11-30-23:59:59 Since it is now 2011-11-27, my output should equal 3. Any help is appreciated. hello guys
i have 2 time variables
$time1 = 22:00;
$time2 = 03:00;
my $current_time is 01:00
how can i figure that $current_time is betwean the $time1 and $time2
the
if ($current_time > $time1 && $current_time < $time2)
works if the day not change!
is any way to ignore the day change
thanks
Edited by lioslios, 09 November 2014 - 10:07 PM. Hi guys, I am trying to do a multidates events availability calender. The script below indicates todays date by highlighting an orange colour and also indicates the start and end date of the event highlighting grey colour on the two dates (The colour are link via css classes as shown). Code: [Select] //Today's date $todaysDate = date("d/m/Y"); $dateToCompare = $daystring . '/' . $monthstring . '/' . $year; echo "<td align='center' "; if($todaysDate == $dateToCompare){ echo "class='today'"; }else{ //Compare's the event dates $sqlcount = "select event_start,event_end from b_calender where event_start ='".$dateToCompare."' AND event_end='".$dateToCompare."'"; $noOfEvent = mysql_num_rows(mysql_query($sqlcount)); if($noOfEvent >= 1){ echo "class='event'"; } } It works ok i.e. if start date = 01/01/2012 and end date = 04/01/2012 both date will be highlighted with grey colour. However I want it to also highlight grey on the dates between the 1st and 4th to show that then anydates between the 1st and 4th are not available and this is when I'm stuck. Please guys I need help. Thanks i want to created a case like the following need help with the syntax: Code: [Select] switch (true) { case($fromdate-$todate==17-04-2001-25-04-2011): echo $finalprice = $result; break; } Hey, I have just recently coded a forum and my topics are ordered by date. This means if there is a topic at 4pm and then there is another topic made at 4:01pm the topic which was posted at 4:01pm will be listed at the top. I know how to add timezones but there is bit of a problem, if person from the UK posts a topic and his/hers timezone is set to: date_default_timezone_set('Europe/London');() this means the time at which the topic was posted will be 20:36pm (Just a randome time example). However if someone posted a topic from america/los_angeles one hour before the one above his/her time will be 11:36 am. Even though this topic was posted one hour before the UK post. The UK topic will override it because it is 8:36pm. In fact, all UK topics will show up at the top and all the america/los_angeles topics will show bellow. How do I slove this problem? I just don't get it even tough I looked online. Please help, thanks. Hi, So I only want my users to be able to perform certain tasks each 12 hours. This is the code I use: Code: [Select] function canVote($ip, $vote_id, $updateTimer = true){ $time = date("Y-m-d H:i:s"); $this->CI->db->where('vote_id', $vote_id); $this->CI->db->where('user_ip', $ip); $this->CI->db->from('votes_voters'); $count = $this->CI->db->count_all_results(); /*$count = $this->CI->db ->where('vote_id =', $vote_id) ->where('user_ip =', $ip) ->from('votes_voters') -count_all_results();*/ $row = $this->CI->db ->where('vote_id =', $vote_id) ->where('user_ip =', $ip) ->get('votes_voters') ->row(); if($count == 0){ $data = array( 'vote_id' => $vote_id, 'user_ip' => $ip, 'last_vote' => $time ); $this->CI->db->insert('votes_voters', $data); return true; } else{ $last_vote = $row->last_vote; if($last_vote + strtotime("12 hours") < $time){ return false; } else{ if($updateTimer = true){ $data = array( 'last_vote' => $time, ); $this->CI->db->where('vote_id', $vote_id); $this->CI->db->where('user_ip', $ip); $this->CI->db->update('votes_voters', $data); } return true; } } } Apparently the failing bit is this: Code: [Select] if($last_vote + strtotime("12 hours") < $time){ return false; } I believe you can guess what I'm trying to do here, if variable 1 + 12 hours is smaller than variable 2, then return false. Any help is much appreciated. Hey guys, What I'm trying to do is set 2 dates. Today's date and then a date 14 days from now. However, I want it to increase the month if by adding 14 days will bring me to the next month, same with the year. I have this, but it doesn't increment the month or year. $week = mktime(11, 59, 59, date("m"), date("d")+14, date("y")); $date = date("Y-m-d H:i:s", $week); Hi guys, I am trying to create a program which manages campaigns. each campaign has a start date and and end date. a user has to enter data releated to the campaign everyday example start date: 10-1-2011 end date: 17-1-2011 now all this information is stored in 2 tables 1st table is "campaigns" this stores the campaign name, start date and end date and 2nd table is "campaign_data" this table stored the data for each campaign and also date of when that data was entered my question is, if the user did not enter data on 11-1-2011 how will I know this, keeping in mind the month diffrence which can accur if the length of the campign was 2 month long? table: campaigns fields: id | campaign_name | start_date | end_date table: campaign_data fields: id | campaign_id | date | page Thank you Hi, I'm trying to get the date, using this code <?php include('config.php'); $today = date('Y-m-d 00:00:00'); $nineyesterday = date('Y-m-d H:i:s', mktime(date("H") - (date("H") + 6), date("i") - date("i"), date("s") - date("s"), date("m") , date("d"), date("Y"))); $now = date('Y-m-d H:i:s'); $nineam = date('Y-m-d 9:00:00'); $ninepm = date('Y-m-d 18:00:00'); if ((strtotime($now) >= strtotime($today)) && (strtotime($now) <= strtotime($nineam))) { //count the answers by yes or no $q = "SELECT opt, count(opt) FROM plus_poll_ans WHERE date<='".$nineyesterday."' AND date>='".$nineam."' GROUP BY opt desc "; } elseif ((strtotime($now) >= strtotime($nineam)) && (strtotime($now) <= strtotime($ninepm))) { //count the answers by yes or no $q = "SELECT opt, count(opt) FROM plus_poll_ans WHERE date<'".$nineam."' AND date>='".$ninepm."' GROUP BY opt desc "; } $q = mysql_query($q) or die(mysql_error()); //separate the results while($row = mysql_fetch_array($q)) { $total_opt[] = $row['count(opt)']; } mysql_freeresult($q); $yes = intval($total_opt[0]); $no = intval($total_opt[1]); $total = $yes + $no; echo $total."<br/>"; echo $yes."<br/>"; echo $no."<br/>"; ?> but it didn't work, why isn't it working? Thanks Hi I'm trying to get this to work. I have known date in MySQL database entered as 2019-08-01 and I need to calculate from that date to current time. What I have is this code $date_ts = strtotime($row['date_ts']); //Timestamp $date_str = date("M-d-Y", $date_ts); //String in format MMM-DD-YYYY $TotalTime = floor((time() - $date_ts)/(60*60*24)) . ' days';//Total in days but my result showing as ==> 18367 days Could someone please help me get this to work so it shows the right number of days. thank you
Evening All, How would I go about selecting a future date based on a date? What I want to achieve is to take todays date, add ten days to it and then store it as the month end following that date. For example 19th Nov + 10days = 29th November = 31st Dec Many thanks for any pointers! Hello Everyone, I seem to be going no place fast... In a PHP file, I should be taking a date from a jquery datpicker, and get it to a format that mysql will read in the yy-mm-dd format with... if(isset($_POST['choice'])) $choice = (isset($_POST['choice'])) ? date("Y-m-d",strtotime($_POST['choice'])) : date("Y-m-d"); Can't seem to get a single inline(embeded) jquery Datepicker date to post right to the PHP and mysql correctly to return data from the table.. My reading has me to understand it should be right, but it is not. Now the datepicker select does have several POST events to PHP that trigger from the single select... data being requsted in PHP by the day, month, and year as well as PHP to use the date to create a highcharts graph in the PHP file to select data for hours of a day, days of a month, and months of a year. The Table is very simple... date, time, power Here is the PHP for dayPower.php that will just collect the sum data for the selected day in the datepicker... <? if(isset($_POST['choice'])) $choice = (isset($_POST['choice'])) ? date("Y-m-d",strtotime($_POST['choice'])) : date("Y-m-d"); $con = mysql_connect("localhost","root","mackie1604"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("inverters", $con); $sql = 'SELECT sum(power) AS power ' .'FROM feed ' .'WHERE date = $choice'; $res = mysql_query($sql) or die('sql='.$sql."\n".mysql_error()); $row = mysql_fetch_assoc($res); echo $row['power']; ?> Here is the javascript that sends the date and how it post the PHP file. Code: [Select] $(document).ready(function () { $('#datepicker').datepicker({onSelect: function(dateText,inst) { var myDate = $(this).datepicker('getDate'); $('#apDiv1').html($.datepicker.formatDate('DD, d', myDate)); $('#apDiv5').html($.datepicker.formatDate('MM', myDate)); $('#apDiv7').html($.datepicker.formatDate('yy', myDate)); $.post('dayPower.php', {choice: inst.val()}, function(data) { $('#apDiv2').html(data).show(); }); $.post('dayGraph.php', {choice: inst.val()}, function(data) { $('#apDiv4').html().show(); }); $.post('monthPower.php', {choice: inst.val()}, function(data) { $('#apDiv6').html(data).show(); }); $.post('monthGraph', {choice: inst.val()}, function(data) { $('#apDiv9').html().show(); }); $.post('yearPower.php', {choice: inst.val()}, function(data) { $('#apDiv8').html(data).show(); }); $.post('yearGraph', {choice: inst.val()}, function(data) { $('#apDiv10').html().show(); }); }}); }); What am I doing wrong Alan I have this code that compares date times but im not sure if its correct. if("2008-08-10 00:00:00.0" > strtotime("now")) { echo "here"; } The code echo's here, but it shouldn't because the time is less than the current time. Any ideas? Been a while since I hit a snag but have a new one! I have hit the old problem that I need to be able to input dates pre 13th December 1901 which appears to not work when using strtotime <?php echo date("F j, Y ", strtotime($row_seasonview['season_start'])); ?> Following a bit of googling it seemed that the alternative was to set the format in the query Code: [Select] SELECT *, DATE_FORMAT(`season_start`,'%D %M %Y') AS startdate, DATE_FORMAT(`season_end`,'%D %M %Y') AS enddate FROM seasons WHERE season_id = %sand to call the date from that <?php echo $row_seasonview['startdate']; ?> but this just comes back with 'Query was empty'. If I remove the two DATE_FORMAT lines the query runs fine. I know there must be a way round this but cannot fathom it out. Any suggestions would be greatfully welcomed!!! Thanks in advance Steve Hello. I wanted to make a table where the dates are listed going forward at least one year. So, at least 365 records, but each date should be unique and should start from today until one year from now. I tried to make a date column and make it a primary key.
However, an error comes up when I try to insert more than one record. This is for a calendar that I'm building so I need the dates listed one year in advance and it would be great if new dates could be added without my having to manually go in there and insert new records to stick to my 1 year in advance rule.
For that aspect I might have to create a php script where the date records are added every time a user logs in, to make sure they are up to date, but anyways what I don't understand is why I can't make a table with 1 year of date records in advance, where each row has a unique date starting from today.
I'm trying to make a script to check a files date time and if its under so many seconds long the page will refresh. here is what I've got. I've been testing the output of the date() functions but when i do the math all i get is a 0. where did i go wrong? is it just i can't do math with dates? Thanks in advanced. Code: [Select] <html><head> <meta http-Equiv="Cache-Control" Content="no-cache"> <meta http-Equiv="Pragma" Content="no-cache"> <meta http-Equiv="Expires" Content="0"> <? $proFile = "profilePics/profile.jpg"; //if ( date ("m d Y H:i:s.") - date ("m d Y H:i:s.", filemtime($proFile)) <= 00 00 0000 00:00:04 ){ //this is where the magic should happen. //echo "<meta http-equiv="refresh" content='4'>"; } ?> </head> <? echo date ("m d Y H:i:s."); echo "<br>"; echo date ("m d Y H:i:s.", filemtime($proFile)); echo "<br>"; echo date ("m d Y H:i:s.") - date ("m d Y H:i:s.", filemtime($proFile)); //only outputs 0 ?> There are two things I want done. 1. Grab the current date in TIMESTAMP form and increase by a certain number of days as assigned by $days() 2. Similar to the previous one, it will grab an already specified TIMESTAMP, and work from there All it echoes is 86400, which I assume is the number of seconds? Code: [Select] <?php $days = 1; echo strtotime("+$days days", strtotime($date)); ?> |