PHP - Strtotime Date Converting
I'm having a bit of trouble converting dates from strings. "171630Z - 181200Z" is what I would like to convert. I want to be able to display more information via date() with strtotime(). The timezone I would prefer is Central. To explain what the string represents:
17 = day (December 17th - today) 1630Z = time in UTC/GMT 18 = day (December 18th - tomorrow) 1200Z = time in UTC/GMT Is there an easy way of doing this or is it impossible with just the day and time in UTC? Thanks. Edit: I would also like to convert "1009 AM CST FRI DEC 17 2010" but that isn't working either. Similar TutorialsHey 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. Hi, I'm struggling a bit with using a date format and strtotime() Code: [Select] if(!empty($_GET['utime'])) { $time = strtotime($_GET['utime']); } utime is supplied in the format dd/mm/yyyy but when I use strtotime() it treats it as mm/dd/yyyy....so 07/12/2011 is taken as 12th Aug rather than 7th Dec..which is the date I want to use. I had a quick ganders through the forums but couldn't find anything specific to this. is there any other attributes to strtotime() that will help, or a similar fucntion or a neat way of swoping my dd/mm/yyyy round before i use strtotime() any help greatly appreciated... Here is the code im using $dateOfBirth = ''; preg_match('|Born on ([a-zA-Z]*\s[0-9]*,\s[0-9]*)\\\u003c\\\/span>|', $page, $match); if($match && count($match)>0) { $date = str_replace(",","",$match[1]); blue($date); $timestamp = strtotime($date); $dateOfBirth = date('Y-m-d',$timestamp); // 1994-03-23 } else { red("No Age is set on profile"); continue; } im getting a match like this March 9, 1993 When i run my script im getting this error [2048] strtotime() [function.strtotime]: It is not safe to rely on the system's timezone settings. Please use the date.timezone setting, the TZ environment variable or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/Chicago' for 'CST/-6.0/no DST' instead Error: [2048] date() [function.date]: It is not safe to rely on the system's timezone settings. Please use the date.timezone setting, the TZ environment variable or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/Chicago' for 'CST/-6.0/no DST' instead What could be the problem?? I am using the following code to get the third sunday of every month for +6 Months from todays date. I have put together the following code which works perfectly when the date is echoed however when it comes to inserting those dates into mysql db it throws a unable to convert to string error. Im new to this and my head is in a spin as to why those dates can't be inserted if they can echoed ok. I have spent 2 days at it now and searched many forum however they seem to get side tracked from my issue (or do they)??????? I figure I can't go mixing DateTime and date() the way I have? Please help??? Code: [Select] [php] $month=date('F'); $now=date('Y-m-d'); $year=date('Y'); $num='3'; //example only this will be a $_POST value 0-3 $day='Sunday'; //example only this will be a $_POST value Monday-Sunday $start= date('Y-m-d', strtotime('+'.$num.' week '.$day.' '.$month.' '.$year.'')); if($start>$now) { $begin=strtotime('+'.$num.' week '.$day.' '.$month.' '.$year.''); } else { $d = new DateTime( $start ); $d->modify( 'first day of next month' ); $nextmonth = $d->format( 'F' ); $year = $d->format( 'Y' ); $begin=strtotime('+'.$num.' week '.$day.' '.$nextmonth.' '.$year.''); } $date=date('Y-m-d', $begin); # insert into db first occurance //mysql_select_db($database); //$query_rsFirst = "INSERT INTO event_date VALUES (NULL, '$event', '$d', '$date', '$date')"; //$rsFirst = mysql_query($query_rsFirst) or die(mysql_error()); echo $date.'<br/>'; for($i = 0; $i <= 4; $i++) { $d = new DateTime( $date ); $d->modify( 'first day of next month' ); $nextmonth = $d->format( 'F' ); $date=date('Y-m-d', strtotime('+'.$num.' week '.$day.' '.$nextmonth.' '.$year.'')); # insert into db //mysql_select_db($database); //$query_rsDate = "INSERT INTO event_date VALUES (NULL, '$event', '$d', '$date', '$date')"; //$rsDate = mysql_query($query_rsDate) or die(mysql_error()); echo $date.'<br/>'; if($nextmonth=="December") { $d = new DateTime( $date ); $d->modify( 'first day of next year' ); $year = $d->format( 'Y' ); } } [/php] hi i have a mysql table with upddate and updtime field as follows: upddate updtime 2011-02-25 11:03:05 2011-01-28 08:01:09 2011-02-20 07:00:08 2011-02-15 06:10:02 2011-02-05 10:21:04 2011-02-09 10:20:25 the data types for upddate is mysql date and for updtime its mysql time In my php page i use Code: [Select] <?php echo $date = date("d M Y",strtotime($row_rs1['upddate'])); ?> to retrieve date and the results displays as 25 Feb 2011. And for time right now im using Code: [Select] <?php echo $row_rs1['updtime']; ?> which displays time as 11:03:05 I want to display time as HH:MM only, how can i do it? OK. So, I am getting a date with the date in m/d/y format and then the hour and the minute. How can I convert this the correct way. This is what I am currently trying: Code: [Select] $started = date("Y-m-d H:i:s", strtotime($_POST['started'] . ' ' . $_POST['started_hour'] . ': ' . $_POST['started_min'])); echo $started; It give me the standard "1969-12-31 17:00:00". When all I tried was $_POST['started'], I at least got the date right. Hi there, i want to convert whatever a user inputs through and html form to a DATE format which when posted will be entered (using mysql) as that particular Date format , i'm using this: Code: [Select] $date = "12-12-11"; //assume this to be the user input $time = strtotime($date); $new_date = date("d/m/Y",$time); echo $new_date; but this format will give the output as: 11/12/2012.....is there a way to convert whatever the user enters to be: dd/mm/yyyy which gives: 12/12/2011 thanks so much 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>"; ?> 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... 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? 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 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 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? 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! 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 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! 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 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 |