PHP - Strtotime() Instead Of Mktime()
how do I use strtotime() instead of mktime()?
$matchTimea = mktime($mHour, $mMinute, 0, $mMonth, $mDay, $mYear); if($matchTimea-time() < $cutoffTimea*60) { Similar TutorialsMy friend a I have to different numbers when we echo mktime(0,0,0,5,12,1990). How is this possible? I thought that only, we you leave some parameters blank, the local this is used, else, it's standardized. thanks in advance New user of PHP here! I'm using it to display entries from my MYSQL database. Image below. This is the code snippet that displays information from the 'name', 'author' and 'date' columns of my database. I learned this technique from w3schools. $result = mysql_query("SELECT * FROM levels ORDER BY date DESC"); while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['name'] . "</td>"; echo "<td>" . $row['author'] . "</td>"; echo "<td align='right'>" . $row['date'] . "</td>"; echo "</tr>"; } I would like to use the date() function to format the date differently. But the date() function requires that I convert my time into a timestamp. I know you have to use the mktime() function for this, but I don't know how. Can someone show me how to pass the contents from $row[date] into the mktime() function? I'm an extreme newbie and have this current error on my site. The error states: Warning: mktime() expects parameter 4 to be long, string given in featured_product.php on line 75 <?php for ($i = 0; $i < $num_rows; $i++) { $id = mysql_result($result,$i,"id"); $title = mysql_result($result,$i,"title"); $featured = mysql_result($result,$i,"featured"); $feature_date = mysql_result($result,$i,"feature_date"); $feature_date_arr = explode("-",$feature_date); $feat_date = mktime(0,0,0,$feature_date_arr[0],$feature_date_arr[1],2000+$feature_date_arr[2]); if ( ($feat_date+($featured*24*60*60))<time() ) { $db2->query("UPDATE product_catalog SET featured = 0 WHERE id='$id'"); $featured = 0; } else { $featured = 1; $db2->query("UPDATE product_catalog SET featured = 1 WHERE id='$id'"); } ?> Any ideas on how to correct this? Thanks! right now i have my date system to say "X amount of seconds ago, X hours ago, X days ago, X years ago, How could i turn mktime() (1290874161) into a mm/dd/yyyy format? so it would say like, "Nov 27, 2010 at Hour:Minute" (whatever the hour/minute is) I'm practising how to do date and time in PHP. I wanted to see if I can get all of the date/time variables from user submission and format them correctly. I went with ISO 8601 date... // From the Manual: // date('c', mktime(1, 2, 3, 4, 5, 2006)); // Prints something like: 2006-04-05T01:02:03+00:00 ...but my attempts always produce a slightly different result for the year, month and timezone... Code: [Select] $inputTime = date('c', mktime((int)$timeHour, (int)$timeMins, (int)$timezone, (int)$dateMonth, (int)$dateDay, (int)$dateYear)); echo $inputTime; // Should return: 2013-11-02T02:30:01+00:00 // Instead returns: 2012-12-02T02:30:01-08:00 The timezone value coming from HTML... <option value="1.0">(GMT +1:00)</option> I noticed; 1. It always picks a year less than whatever year I select. 2. It always selects 12 as the month. 3. It always adds -08:00 after the picking the correct timezone. How come? Thanks in advance. Or am I just going crazy? OK, I've been running a query that uses the snippet below to get the previous month. It's : Code: [Select] $sql = "SELECT COUNT(`id`) as `counted` FROM `click_tracking` WHERE `date` LIKE '". date('Y-m', mktime(0, 0, 0, (date('m')-1), date('d'), date('Y'))) ."-%'"; Here's a snippet for people to try: Code: [Select] date_default_timezone_set('America/Toronto'); echo date('Y-m', mktime(0, 0, 0, (date('m')-1), date('d'), date('Y'))); //prints 2012-03 (March, which is incorrect; should be February- 2012-02) That should have printed 2012-02, but instead, it prints 2012-03. If I modify the mktime() to (date('m')+1): Code: [Select] date_default_timezone_set('America/Toronto'); echo date('Y-m', mktime(0, 0, 0, (date('m')+1), date('d'), date('Y'))); //prints 2012-05 (May, which is incorrect; should be April - 2012-04) It prints 2012-05 which is incorrect for the given timezone. Should be 2012-04 I'm thinking it's a leap year bug and will work itself out tomorrow (per my timezone, anyways). So, when I use the mktime() function, it seems to be thinking we're in April already. A standard date('Y-m') prints the correct YYYY-MM (2012-03) though. Am I tripping out? Hi, I'm getting this error: Warning: mktime() expects parameter 5 to be long, string given in /home/user/public_html/include/functions.php on line 58 The code is: if($Date_Format == 1) #For dd-mm-yyyy { $Date_Output = date("d-m-Y",mktime(0,0,0,$Month,$Day,$Year)); } elseif($Date_Format == 2) #For dd/mm/yyyy { $Date_Output = date("d/m/Y",mktime(0,0,0,$Month,$Day,$Year)); } elseif($Date_Format == 3) #For mm-dd-yyyy { $Date_Output = date("m-d-Y",mktime(0,0,0,$Month,$Day,$Year)); } elseif($Date_Format == 4) #For mm/dd/yyyy { $Date_Output = date("m/d/Y",mktime(0,0,0,$Month,$Day,$Year)); } elseif($Date_Format == 5) #For Mon Day YYYY { $Date_Output = date("M D Y",mktime(0,0,0,$Month,$Day,$Year)); } elseif($Date_Format == 6) #For Month Day YYYY { $Date_Output = date("F D Y",mktime(0,0,0,$Month,$Day,$Year)); } elseif($Date_Format == 7) #For Date"nd" Month YYYY { $Date_Output = date("d\\t\h F Y",mktime(0,0,0,$Month,$Day,$Year)); } elseif($Date_Format == 8) #For Month Day YYYY { $Date_Output = date("F d Y",mktime(0,0,0,$Month,$Day,$Year)); } elseif($Date_Format == 9) #For Month Day YYYY { $Date_Output = date("d/m/Y H:i",mktime($DateDispsplittimehr,$DateDispsplittimemin,0,$Month,$Day,$Year)); // Line 58 } return $Date_Output; } else { return NULL; Line 58 says $Date_Output = date("d/m/Y H:i",mktime($DateDispsplittimehr,$DateDispsplittimemin,0,$Month,$Day,$Year)); I really appreciate any help on this. 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! 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 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,
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 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... 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 want to have a couple of different formats from mktime(), which the value could be: 1292183335. I would like to have some of the formats like: Tuesday at 12:00am, Wednesday, January 12, 2011 at 12:00am Also taking different timezones in consideration. I'm not sure what to use for that. 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 what is the propper way to write Code: [Select] $payrollweek="10"; date('Y-m-d', strtotime("+($payrollweek - 2) week $payrollend") ), "<br />"; its the ("+($payrollweek - 2) that isnt working i think 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! 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. Hi I am new to PHP and trying to convert a date from a registration form (in the form 01/01/2011) to Y-m-d so that it can be stored in the database. This is the code I have, pretty sure it worked before but not it has stopped working! Any ideas? $dateformat = $_POST['dob']; $correctformat = date('Y-m-d',strtotime($dateformat)); I have checked what $_POST['dob'] is printing and that is correct, however the $correctformat is printing 1970-01-01 everytime. Any help would be greatly apreciated. Thank you |