PHP - How Can I Make Strtotime() More Accurate ?
Hi - I have an HTML form where the customer can amend text dates. However, I have noticed that if you for example change 01 Jun 2012 to something like, 01 July 2012, 50% of the occasions, strtotime() will ignore the change and just go back to 01 jun 2012.
This is not unique to June, is there anything I can do to make things better ?? Thanks ! Similar Tutorialshey, i'm working on an online dictionary for my native language. i noticed when searching, the results are sometimes inaccurate. this is my query. Code: [Select] SELECT * FROM words WHERE word LIKE '%$q%'" for example, if i search for "tive" (supposing this is a real word) it doesnt return the word tive and will return "native" because it occurs first in the database. so i suppose basically i'm looking for a way to sort them by relevancy? I am trying to calculate the date and time difference between two timestamps. I am using this for an auction style website and I thought it was working fine until I woke up this morning and it said that there was only 1 hr left in auction when I actually started a 24 hour auction at like 7pm last night. //trying to find date diff accurately Code: [Select] $now = mktime(date("g"), date("i"), date("s"), date("m") , date("d"), date("Y")); $end = $timestamp; $start = new DateTime(); $start -> setTimestamp($end); $current = new DateTime(); $current -> setTimestamp($now); $interval = date_diff($current, $start, true); $days = $interval ->format('%d'); $hours = $interval ->format('%h'); $minutes = $interval ->format('%i'); $seconds = $interval ->format('%s'); This is how I created the timestamp to store in the database for auction. Code: [Select] //Creates timestamp for when auction ends using auction //length chosen. $timestamp = mktime(date("g"), date("i"), date("s"), date("m") , date("d")+$auction, date("Y")); the php web video script that I'm trying to modify allows Users to purchase videos successfully, however, the video price that is reflected in the db ('u_paid_videos') table (where transaction info is stored) appears to show the default video price (video_play_price from the 'config' db table) every time, instead of the accurate video price. I'm not sure if this code needs to be tweaked so that the actual price will show in the 'video_play_price' column (in 'u_paid_videos' table): // get cost video // get the default video price, to use if there is no per video play price $db->where('name', 'video_play_price'); $db_cost = $db->getOne('config'); $video_cost = (float)$db_cost->value; // the number of submitted videos - used to determine if all records were inserted $count_video = count($id_array); $user_id = $user->id; $wallet = (float)str_replace(',', '', $user->wallet); $balance = (float)str_replace(',', '', $user->balance); // add up the video prices $amount = 0; foreach ($id_array as $id) { $video_id = (int)PT_Secure($id); // get video data $video = $db->where('id', $id)->getOne(T_VIDEOS); // add the video play price if any, or the default price $amount += $video->video_play_price?$video->video_play_price:$video_cost; } // determine if the user has enough credits if( ($wallet >= $amount) OR ($balance + $wallet >= $amount) ) { $db->startTransaction(); $inserted_records = 0; foreach ($id_array as $id){ $video_id = (int)PT_Secure($id); // get video data $video = $db->where('id', $id)->getOne(T_VIDEOS); // use the video play price if any, or the default price $video_cost_new = $video->video_play_price?$video->video_play_price:$video_cost; $uploader_amount = $video_cost_new *0.50; // add data to paid table $insert_buy = $db->insert('u_paid_videos', [ 'id_user' => $user_id, 'id_video' => $video_id, 'session_key' => $_SESSION['session_key'], 'video_play_price' => (string)$video_cost, 'video_title' => $video->title, 'user_id_uploaded' => $video->user_id, 'earned_amount' => $uploader_amount ]);
On my search result page, I wanted to search for "Nintendo DS Lite - Pink" I used following code: Ex: Code: [Select] $search_text = "Nintendo DS Lite Pink"; $kt=split(" ",$search_text);//Breaking the string to array of words // Now let us generate the sql while(list($key,$val)=each($kt)) { if($val<>" " and strlen($val) > 0) { $q .= " name like '%$val%' or "; } }// end of while //Remove the last 'OR' $q=substr($q,0,(strlen($q)-3)); Than the $q would be: Code: [Select] SELECT * FROM `products` WHERE name like '%Nintendo%' or name like '%DS%' or name like '%Lite%' or name like '%Pink%' And i am getting Mysql Output given below: 1) Activity Meter - DS. 2) Nintendo DS Red. 3) Nintendo DS Lite Pink. 4) Nintendo DS Lite Turquoise. But the third result is most accurate/relevant then first two result. Please help me out to get the most accurate row first then the relevant rows as per their relevancy with search term "$search_text" Many Thanks in Advance. Hi, I have a bit of an issue with my application, I didn't notice until today, Sunday... Apparently PHP sees Sunday as the first day of the week, which I cannot fathom where it got hat notion from... but anyway, so I have the following samples of my code: Code: [Select] $mondayoflastweek = date('Y-m-d H:i:s', strtotime('monday last week')); $sundayoflastweek = date('Y-m-d 23:59:59', strtotime('sunday last week')); The crazy and very troublesome thing about the results of that code is that if you run that today, Sunday, it shows today as last week... So therefore, the next bit of code is just as problematic: Code: [Select] $mondayofthisweek = date('Y-m-d H:i:s', strtotime('monday this week')); $sundayofthisweek = date('Y-m-d 23:59:59', strtotime('sunday this week')); As you can imagine, for payroll, this is causing a big problem... My week starts at precisely 12:00 am on Monday and ends at precisely 11:59 pm on Sunday... Could someone please help me out with this... So, my problem is that I need to edit my Xmas calculator to understand when this year's Xmas is over, it will automatically jump to the next one (2011-12-25). I have no idea how to do this (noob to php...) Thanks in advance. Here's my code: Code: [Select] <?php $time=time(); $xmas=strtotime("2010-12-25 00:00:00"); $diff = $xmas - $time; $days=intval($diff/86400); $left=$diff%86400; $hs=intval($left/3600); $left=$left%3600; $mins=intval($left/60); $secs=$left%60; echo "<font size=12 face=corbel> Xmas is after:<br> <strong>$days days</strong><br> <strong>$hs hours</strong><br> <strong>$mins mins $secs secs</strong>! </font>"; ?> Hello! A user is going to input a date in the form "mm/dd/yyyy". I'd like to convert this to a time stamp so that I can store it via MySql. I've heard about the "strtotime" function in PHP but I'm not sure how it can tell that the user 03/04/2011 represents March,4th 2011 and not, say April,3rd 2011 (European version). Help with the correct syntax for conversion would be greatly appreciated. Thank you, Eric First time posting code and very new to php aka learning as I go... The below code works as shown, but in the 'else' clause, I'd like to replace the +4 with a variable. How to do this? I've tried, Code: [Select] $renewaldate = strtotime($subscriptiondate); $sft = "' +" . $duesmultiplier . " year'"; $next_year = date('Y-m-d',strtotime($sft,$renewaldate)); but this doesn't return what I expect. Perhaps this can't be done using this technique? Code: [Select] if($n == 0) { $renewaldate = strtotime($subscriptiondate); $next_year = date('Y-m-d',strtotime('next year',$renewaldate)); echo $next_year; } else { $renewaldate = strtotime($subscriptiondate); $next_year = date('Y-m-d',strtotime('+4 year',$renewaldate)); echo $next_year; } Thank you I'm trying to add 5 days to $today date: Code: [Select] <?php $exp_date = "2011-09-11"; $todays_date = date("Y-m-d"); $today = strtotime("+5 days", $todays_date); $expiration_date = strtotime($exp_date); if ($expiration_date > $today) { echo "Valid: Yes"; } else { echo "Valid: No"; } ?> Is +5 days used wrong? when i use strtotime('+3 HOURS') everything is fine. so why cant i use strtotime('+3.5 HOURS')? what would be the proper way to do this? Hello,
I'm trying to use strtotime to add time to a mysql type timestamp. Any help would be appreciated. I think I have tunnel vision from looking at it when I know it has to be something obvious...lol
$start = "2014-05-22 09:16:24"; $type = 30; $endtime = date($start,strtotime("+ '.$type.' minutes")); echo "End: ".$endtime."</br>Start: ".$start;Right now it returns: End: 2014-05-22 09:16:24 Start: 2014-05-22 09:16:24 Little confuzzled. I have got a bunch of listings that will be printed at the end of the page but I need to subtract a time specified in a textbox (send it to itself) on the page. Code: [Select] $row['active'] = (strtotime('$row[etd]') + $rwy1 = $_POST['tmatttextfield']);Well this just converts the time in the database which is always going to be a four character number such as: 1300 for 1PM into a time and adds the textbox value to the time. Code: [Select] <td width="50" bgcolor="<?php echo $bc?>"><strong><?php echo date('hi', $row['active']);?></strong></td>That just prints it all out formatting the time to only Hours and Minutes. So just to clarify the user would put in say '10' in the textbox which would constitute 10 minutes. The code above should then define that '10' as 10 minutes. Define the database result, ie. 1500 as 3PM and add the 10 minutes to it so it would become 1510. I have been getting some wierd and wonderful results whilst trying to get it to work. Mainly just 1200 though. Harry. how do I use strtotime() instead of mktime()? $matchTimea = mktime($mHour, $mMinute, 0, $mMonth, $mDay, $mYear); if($matchTimea-time() < $cutoffTimea*60) { I have this php code: length is 15 , that is what it is coming out of the database $length = $get['length']; $time = date('Y-m-d', strtotime('+$length days')); But when I echo time it comes out with this result 1969-12-31 What am I doing wrong? can someone please tell me how to echo the current time or date? I have the date in the format 1306768978 in the database but I dont want to echo it back that way obviously. Not only that but does anybody know of a good tutorial on how to use this function? so i have a basic html table and down the left column i have monday,tuesday,wednesday.... sunday. In the column 2nd from the left, i want to fill it with the current weeks dates, which is really based on the current date. So if today is Jul 29th Friday, then strtotime("last monday") would give me Jul 26th Monday... my issue is "last monday" wont work if today is monday because that will give me 19th of july... and since I technically dont know what day is today i'm not sure how to cleanly work around this without having to use some if statement... if today is monday do "last monday +7 days" or just do "now" etc... Thanks! Hey freaks, running into a little issue with the use of date and strtotime.... basically I wanna present the date in the format m-d-y to the user and insert it into the DB in the Y-m-d format. What seems to be happening is a freaky Y-d-m format and I don't know why this is. This code is an example that expresses my frustration. Code: [Select] $nmon = strtotime("next Monday"); $next = date('m-d-Y', $nmon); $_SESSION['REPORT_DUE'] = $next; $nfri = strtotime("Friday"); $due = date('m-d-Y', $nfri); $_SESSION['WEEK_ENDING'] = $due; $ses1 = $_SESSION['WEEK_ENDING2'] = date('Y-m-d', strtotime($due)); $ses2 = $_SESSION['WEEK_ENDING3'] = date('d-m-Y', strtotime($ses1)); .....output.... Quote [WEEK_ENDING] => 09-02-2011 [WEEK_ENDING2] => 2011-02-09 [WEEK_ENDING3] => 09-02-2011 ) Thank you for any assistance. hello, what am i doing wrong? Code: [Select] $starttime=$row['starttime']; $esttime1=date( 'g:i a', strtotime(-2 hours, $starttime) ); Good Day, I'm struggling to get strtotime() to return anything. I have the following code.. $fields['birth']['day'] = 9; $fields['birth']['month'] = 7; $fields['birth']['year'] = 1982; $dob = $fields['birth']['day'].' '.$fields['birth']['month'].' '.$fields['birth']['year']; // Returns: 9 7 1982 $dob = strtotime($dob); die($dob); No matter what I do $dob isn't returning anything once strtotime() is used. I've tried matching my day/month/year 's with examples in the manual on the strtotime() page but using date() on the fields, but it still doesn't want to return anything. I feel I'm missing something simple here. Any help is appreciated. Regards, Ace Hi! the following code sucessfully outputs a timestamp. Code: [Select] $datealt=strtotime('2012-03-13 ,23:13:00'); The following however does not: Code: [Select] $datealt=strtotime('$date ,$hours:$minutes:00'); I have checked if the variables echo out and they do.. any clues much appreciated x |