PHP - Date Conversion Function
I am working on a project where I need PHP and mysql to perform calculations on dates, so I am storing them in the mysql format 2011-09-12, I am writing a custom function that will convert this into a date('M d Y') format, but all I get when I run my function is "1" This is my first custom function, so i am sure it is a simple fix, I am just at the end of my spectrum on functions. I can do this as an inline script, but will need this on various pages
Code: [Select] function readableDateConvert($mysqlTime) { $phpTime = strtotime($mysqlTime); $readableTime = date('M d Y',$newTime); return $readableTime; } Similar TutorialsCan someone tell me how to convert a MySQL date such as 2010-08-13 into the the US format, such as 8/13/2010, and also the reverse using PHP? I can't seem to find the right functions. Thanks! //$start_point = '2014-07-22'; $lastday = date('t',strtotime($start_point)); $month_val = date('n',strtotime($start_point)); echo "Last day: ". $lastday . "<br/>"; echo "Month: ". $month_val . "<br/>";I have a problem with a date value read from a MySQL database table into a PHP variable but when manipulating the vale to determine the month of the date, unexpected results appear. I am not sure what causes this but I suspect PHP is not properly handling date to string conversion. The output from the code shows 1 as the month when it should be 7. Hower the line of code with the comment "//$start_point = '2014-07-22' if added in give the correct result. When the value of the variable "start_point" is printed, it gives the value "2014-07-22". Please see the code Telly I am writing an app that takes ofx banking files files on convert them to csv for excel or importation into sql. I am on the last task. Converting the nonstandard date:time stamp. I am using preg_replace(). I am not the best at regular expressions. I am getting this error: "Warning: preg_replace() [function.preg-replace]: Unknown modifier '\' in C:\xampp\htdocs\banking\ofx-date.php on line 13" CODE: // Date Time 20110228000600 (2011/02/28/, 00:06:00) // Expected Return '<DTPOSTED>2011/02/28,00:06:00' $ofxDate = '<DTPOSTED>20110228000600'; $ofxNewDate = substr($ofxDate,0,10) . substr($ofxDate,10,4) . '/' . substr($ofxDate,14,2) . '/' . substr($ofxDate,16,2) . ',' . substr($ofxDate,18,2) . ':' . substr($ofxDate,20,2) . ':' . substr($ofxDate,22,2); echo htmlentities($ofxNewDate) . PHP_EOL;; $patternA = '/<DTPOSTED>/\b[0-9]+\b'; //$replacement = '/<DTPOSTED>/'; $stringB = preg_replace($patternA, $ofxNewDate, ofxDate); echo htmlentities($StringB) . PHP_EOL; hey guys the date stored in mysql is yyyy-mm-dd i need to convert it to do a string compare i have used the following $query = "SELECT * FROM booking where id=$sel"; $result = mysql_query($query); while ($row = mysql_fetch_array($result)) { $num1=$row['no_rooms']; $from1=$row['from']; $to=$row['to']; $sel1=$row['room_type']; $date = date('d-m-y', strtotime($from1)); } echo "$date"; it prints 01-01-70 can any1 tell me where is the error? thanks I'm pulling some info from various XML files and am having problems converting the following date format so that it can be stored as a DATETIME field in a MySQL database. Tuesday,17 August 2010 09:25:00 This obvious newbie would appreciate a shove in the right direction... Thanks in advance I'm getting this Time Zone error. Perhaps it's a compatibility issue with PHP 5.3. Looked all over for an answer without finding one. Here is the error message Warning: date() [function.date]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting 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/New_York' for 'EST/-5.0/no DST' instead in /blocked.php on line 41 12/02/12 Here is the code. Line 41 is near the bottom, the one with the d,m,y. Perhaps the echo date (d/m/y") needs to be changed. Appreciate any help! Code: [Select] <table border="3" width="16%" align="center" cellspacing="0" bgcolor="#FF6600" bordercolor="red" bordercolordark="red" bordercolorlight="red"> <tr> <td width="176"> <p align="center"><?php // shows IP Number on Page echo $ip; ?> </p> </td> </tr> </table> <p align="center"><?php // Show the user agent echo 'Your user agent is: <b>'.$_SERVER['HTTP_USER_AGENT'].'</b><br />';?></p> [b]<h1 align="center"><?php echo date("d/m/y");?></h1>[/b] </td> </tr> </table [,code] I am scraping Birthdays from a webpage that are in this format September 15, 1987 $age = 15; $page = source code of a html page; so far i have made this function How do i return the function as true if the date scaped makes the person over 15 and false if the person is younger? function checkForAge($page,$age) { preg_match('|Born on ([a-zA-Z]*\s[0-9]*,\s[0-9]*)\\\u003c\\\/span>|', $page, $match); if($match && count($match)>0) { echo "Match Found"; $dateOfBirth = str_replace(",","",$match[1]); // $dateOfBirth = date('d/m/Y', strtotime($dateOfBirth)); $dateOfBirth = strtotime($dateOfBirth); /* if("date of birth makes person over $age) { return true; } else { return false; } */ } i just want to display the values form database which i have saved before and just want to be displayed when specific time comes as i told above in e.g if i have stored some thing like value let say ( 10,20,30,40 and so on ) in database now i just want a php code to display it on site but on specific time only like this, i time = 10:00am display value = 10 when time = 10:30am display value = 20 when time = 11:00am display value = 30 and so own simple. Hello Again guys, I have varous dates stored in a database , these dates can look like this 2010-08-06 07/08/2010 07-08-2010 The problem is I dont know what format they are stored in.. What I need is a function which takes in 1 date at a time and returns it in a format I need it to be.. the format I need is 07/08/2010 thank you again for all you help I have been looking at thr date() but not been able to answer my qustion yet Hi all, I want to include the date and time in the web page to display when a user logged into the site. I found a code.I want to know how it could modify with relevant to our local time? <?php $today = date("d M Y h:i A"); echo $today ?> Thanks, $date=date("Y-m-d H:i:s"); This is returning me the GMT time instead of EST. I get 15:26:08 as the time instead of 10:26:08. How can I get around this? I am doing somthing complicated with dates and I think I am making it harder than I should. I need to subtract "x" number of months from the current date, then create 2 dates in that month that represent the first day, and last day of the month. Then make it in a format that SQL can read to get the SUMS I need between those dates. My last 2 strtotimes are wrong and are giving me the 1969 dates. Code: [Select] function commiss($nummonth){ $repnum = $_SESSION['REPID']; $date = date("Y-m-d", strtotime("-" . $nummonth . "")); $time2 = " 00:00:01"; $time = " 23:59:59"; $date1 =date("Y-m-d", strtotime(date('m').'/01/'.date('Y'))); $date2 =date("Y-m-d", strtotime('-1 second',strtotime('+1 month',strtotime(date('m').'/01/'.date('Y'))))); $date1 = date("Y-m-d", strtotime("-" . $nummonth . "", $date1)); $date2 = date("Y-m-d", strtotime("-" . $nummonth . "", $date2)); $date1 = $date1 . $time2; $date2 = $date2 . $time; $string = "SELECT SUM(premium), SUM(repcom) FROM commission WHERE repnum = '$repnum' BETWEEN '$date1' AND '$date2'"; $sqltotal = mysql_query($string)or die (mysql_error()); $sqlprem = mysql_fetch_array('[SUM(premium)]'); $sqlcomm = mysql_fetch_array('[SUM(repcom)]'); echo "Premium: " . $sqlprem; echo "\n Rep Commission: " . $sqlprem; } How would you use the date function to display a birthday with an user's input? Hi, I have the following code which lists all the dates since October 5th 2007 until yesterday's date, and displays it as a link. This part works fine. What I want to do is omit the weekends, so the first few lines would read like so: 05/10/2007 08/10/2007 09/10/2007 10/10/2007 11/10/2007 12/10/2007 15/10/2007 Does anyone know how I can modify this code, or have any other methods of doing this? Code: [Select] <?php function dates_between($start_date, $end_date = false) { if ( !$end_date ) { $end_date = date("Y-m-d"); } $start_date = is_int($start_date) ? $start_date : strtotime($start_date); $end_date = is_int($end_date) ? $end_date : strtotime($end_date); $end_date -= (60 * 60 * 24 + 1); $test_date = $start_date; $day_incrementer = 1; do { $test_date = $start_date + ($day_incrementer * 60 * 60 * 24); echo '<a href="http://markets.ft.com/RESEARCH/markets/DataArchiveFetchReport?Category=CU&Type=WORL&Date=' . date("m/d/Y", $test_date) . '">' . date("d/m/Y", $test_date) . "\n</a><br>"; } while ( $test_date < $end_date && ++$day_incrementer ); } dates_between("2007-10-04"); echo "\n\n"; ?> Thanks, Dave Hi, I am using this function to output a list of dates for every friday over the next 12months. This is working fine. Code: [Select] [php] //function function nextWeeksDay($date_begin,$nbrweek) { $nextweek=array(); for($i = 1; $i <= $nbrweek; $i++) { // 52 week in one year of course $nextweek[$i]=date('Y-m-d', strtotime('+'.$i.' week',$date_begin)); } return $nextweek; } /// end function /// example of a select date // var $date_begin=strtotime('this Friday'); $nbrweek=52; // call function $result=nextWeeksDay($date_begin,$nbrweek); // Insert //$date1=date('Y-m-d', $date_begin); for($i = 1; $i <= $nbrweek; $i++) { //$date=$result[$i]; echo $result[$i]."<br/>"; } [/php] My problem is that when I alter the code to try and get the date for every second Friday for the next year the script freezes. Code: [Select] [php] //function function nextWeeksDay($date_begin,$nbrweek) { $nextweek=array(); for($i = 2; $i <= $nbrweek; $i+2) { // 52 week in one year of course $nextweek[$i]=date('Y-m-d', strtotime('+'.$i.' week',$date_begin)); } return $nextweek; } /// end function /// example of a select date // var $date_begin=strtotime('this Friday'); $nbrweek=52; // call function $result=nextWeeksDay($date_begin,$nbrweek); // Insert //$date1=date('Y-m-d', $date_begin); for($i = 2; $i <= $nbrweek; $i+2) { //$date=$result[$i]; echo $result[$i]."<br/>"; } [/php] I really don't understand why?? Does anyone know why? Thanks Hi, I'm making an RSS feed http://dev.subverb.net/mixes_rss.php and as you see the date('r', $row['date']) function is not converting it. I've echo'd $row['date'] into the description to show what format the date is in (just mysql datetime. Am I missing something? Thanks hello i got this small function i`m working on to get the date of a month something like get current date - 1 but not sure how to handle jan month can someone help me please ? $month = date('m') - 1; $date_startt = date('Y').'-'.$luna.'-01'; $timestamp_start = strtotime($data_start); Hi there, what I have been trying to do is add some additional logic.. My problem is I want to stop displaying the month and day after the year 2000? I know I need to add an if and else statement but this is my first actual project and I am a little stuck.. here is the page, it's a plugin for a timeline http://www.llandover...oject-timeline/
And the file is attached below.. any help would be appreciated. The plugin code was too long to just post in here, didn't want to cause any slow loading issues for people on a slow connection.. Thanks and I appreciate any help.
Attached Files
annual_archive.php 18.15KB
5 downloads want something like: Code: [Select] date('F j, Y, g:i a') but in central timezone, how do I change the timezone to central? I cant figure out how to add $recieved into this needs to take place of time hope someone knows how to do this what im doing is adding in the UNIX time i want to offset Code: [Select] <?php $offset = date('m/d/Y/H:i:s', time()+$send1['time_offset']); ?> |