PHP - Time Ago Function Always Referring To January 1st 1970
Hello,
When I use this function, the time always refers to January 1st 1970 ?! How come? I get the output '4 decades 1 year 11 months' regardless of which date I enter into the function.. ? Code: [Select] function timeAgo($tm,$rcs = 2) { $cur_tm = time(); $dif = $cur_tm-$tm; $pds = array('second','minute','hour','day','week','month','year','decade'); $lngh = array(1,60,3600,86400,604800,2630880,31570560,315705600); for($v = sizeof($lngh)-1; ($v >= 0)&&(($no = $dif/$lngh[$v])<=1); $v--); if($v < 0) $v = 0; $_tm = $cur_tm-($dif%$lngh[$v]); $no = floor($no); if($no <> 1) $pds[$v] .='s'; $x=sprintf("%d %s ",$no,$pds[$v]); if(($rcs > 0)&&($v >= 1)&&(($cur_tm-$_tm) > 0)) $x .= timeAgo($_tm, --$rcs); return $x; } When I call for the function I use this: Code: [Select] timeAgo($row['dateposted']) The format in the database for 'dateposted' is: Code: [Select] 2006-08-14 23:29:23 Any ideas what I am doing wrong? Thanks a million Similar TutorialsHey Guys, I am fixing someone else's code, so please bear with me on this one. Basically the site has an application form and once the user has submitted the form, they get an automated email with an induction date, and the date is taken from the admin panel where the admin user puts in the date and time and number of applicants. So basically the if the admin user puts in Date: 23th Sept 2011 Time: 9:30 Number of Applicants: 30, the next 30 successfull applicants will get that date and time. The problem with the code is that now whenever the user fills in the application and their application is successfull, they are getting an induction date of Thursday, 1 January 1970 and no time. I am not sure why this happens if someone could point me in the right direction. Here is the snipet of the code where it prints out the date: Code: [Select] Your Induction date is <?php echo (date("l, j F Y", strtotime($array_result_ind['date'])))." at ".$array_result_ind['hour'];?> The code at the top of the page is: Code: [Select] <?php //include('browser_redirect.php'); //Start session and page session_start(); $induction = isset($_GET['id'])?$_GET['id']:''; $_SESSION['change_ind_date_app_ID'] = $induction; $db_leafletj_jobs = mysql_connect("localhost", "leafletj_jobs", "t10872") or die(mysql_error()); $sel_leafletj_jobs = mysql_select_db("leafletj_jobs") or die(mysql_error()); $query_str = "SELECT applicants.id, applicants.unique_ref, applicants.app_date, applicants.fname, applicants.surname, applicants.ind_date, applicants.ind_ord_no, inductions.date, inductions.hour FROM (applicants JOIN inductions ON (applicants.ind_date = inductions.date)) WHERE applicants.id=".$induction; $result_ind = mysql_query($query_str, $db_leafletj_jobs) or die(mysql_error()); //$count = mysql_num_rows($result_ind); $array_result_ind = mysql_fetch_array($result_ind, MYSQL_BOTH); ?> Also the database for the induction date and time is blank. I haven't designed the application so I am a little lost. I the system was all working a couple of weeks ago but now all of a sudden it prints out that 1970 date with no time. Is this a quick fix? Thanks, Ken Hey guys, I am having a bit of trouble, I am using the function time() to insert the time the form was submitted to the database. It inserts the data like this: 1301407145 I have a date column. I am then trying to echo the date the person submitted the form from the database using: <?php echo date('d-M-Y h:i A', $date); ?> the $date gets used with the mysql fetch array and for some reason it outputs it as: 01-Jan-1970 01:00 AM Can someone explain to me why? I am using this to insert into the db. $date = time(); and then the $date goes into the statement. mysql_query("INSERT INTO messages (reciever, sender, subject, message, date) VALUES ('$reciever', '$user', '$subject', '$message','$date')") or die (mysql_error()); Why isnt it working? PHP date and time function is not showing correct time on my local system I have the following php code date_default_timezone_set("Africa/Lagos"); $date = date('d-m-y h:i:s'); echo "Server Time ".$date ."<br>"; echo "The time is " . date("h:i:sa")."<br>"; $current_datetime = date("Y-m-d") . ' ' . date("H:i:s", STRTOTIME(date('h:i:sa'))); echo "Current time1: ".$current_datetime . "<br>";
Output
Server Time 21-05-21 09:55:39
Expected Output
Server Time 21-05-21 10:55:39
Any help would be appreciated. Edited May 21 by Ponel I have a php page where i need to list all item in a particular month.But the format for date stored is MMMM DD,yyyy that January 22, 2011. How do i extract the month name only from the above format Hi i have a form which display the date in the format that i want which is m/d/yyyy but then when i try update it sets the date back to 01/01/1970 help please this how the update script looks like <?php //Get the key field to be amended $news_id = $_GET['news_id']; $news_date = date('Y-m-d', strtotime($_GET['new_date'])); $subject = $_GET['subject']; $news_artical = $_GET['news_artical']; // check if there were any errors // check if there were any errors //$news_date = STR_TO_DATE('$news_date','%Y/%m/%d'); $query = "UPDATE news SET news_date = '$news_date', subject='$subject', news_artical='$news_artical' WHERE news_id = '$news_id'"; mysql_query($query) or die(mysql_error()); // execute query print "<p>The following records has been updated: </p>"; $result = mysql_query($query) ; //if there was a problem - get the error message and go back if (!$result) { echo "There were errors :<br>". mysql_error(); } else //OK, then the insertion was successful { //Create a new query to display the new row in a table $query = "SELECT news_id, news_date, subject, news_artical, DATE_FORMAT(news_date, '%d/%m/%Y') as new_date FROM news WHERE news_id = '$news_id' "; $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); echo "<table cellpadding=10 border=1>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>".$row["news_id"]."</td>"; echo "<td><strong>" .$row["new_date"]."</strong></td>"; echo "<td><strong>".$row["subject"]."</strong></td>"; echo "<td width='55%'>".$row["news_artical"]."</td>"; echo "</tr>"; } //End while echo "</table>"; } //End Else insertion successful //End else successful Amendment ?> Hello. I'm trying to create a function that will turn my timestamp into something like "9 hours ago", "3 days ago", ect.. so far I have the below function which seems to do the job fine. only problem is when i try to include it within a span it always places itself right before it. I use Code: [Select] echo "<span class=\"smalltext\">".time_stamp($date)."</span>"; witht the following function Code: [Select] function time_stamp($session_time) { $time_difference = time() - $session_time ; $seconds = $time_difference ; $minutes = round($time_difference / 60 ); $hours = round($time_difference / 3600 ); $days = round($time_difference / 86400 ); $weeks = round($time_difference / 604800 ); $months = round($time_difference / 2419200 ); $years = round($time_difference / 29030400 ); // Seconds if($seconds <= 60) { echo "$seconds seconds ago"; } //Minutes else if($minutes <=60) { if($minutes==1) { echo "one minute ago"; } else { echo "$minutes minutes ago"; } } //Hours else if($hours <=24) { if($hours==1) { echo "one hour ago"; } else { echo "$hours hours ago"; } } //Days else if($days <= 7) { if($days==1) { echo "one day ago"; } else { echo "$days days ago"; } } //Weeks else if($weeks <= 4) { if($weeks==1) { echo "one week ago"; } else { echo "$weeks weeks ago"; } } //Months else if($months <=12) { if($months==1) { echo "one month ago"; } else { echo "$months months ago"; } } //Years else { if($years==1) { echo "one year ago"; } else { echo "$years years ago"; } } } in the end I get the span tag with nothing in it and the text right before it. What am I doing wrong? I tested each variable individually and it appears that the $format variable is causing problems everytime I plug it in. What am I doing wrong here? //$time is pulled from the db in the timestamp format //$tzone is a value like +1 hour, +3 hours etc. //$format is a value like M.d.Y h:i A function converttime($time){ return '<i>'.date($format, strtotime($time . $tzone)).'</i>'; } Hello everyone, I'm having real trouble with something I'm sure most of you would find easy.... ...here we go..... ...I want a redirect script based on referring URL. More specifically, a small part of the referring URL. example.... ad is placed on Bing, if someone clicks on the ad they get directed to a URL starting "0.r.msn.com/?Id=54r645f3345e" which redirects to my site. I want the referrer checker to looks specifically for the ".r.msn.com/?Id=" and if it's present let the visitor through to the site. The reason I only want it to check for that small part of the URL is because the bits before and after are variables and that is the only "static" part. However, if someone arrives at my site by not clicking on an ad, I want them to go somewhere completely different, let just say for arguments sake, google.com. This is what I have so far... <?php $_GET = $_SERVER['HTTP_REFERER']; if ($_GET == '.r.msn.com') { header('Location: http://www.mysite.com'); } else { header('Location: http://www.google.com'); } ?> I must be missing something, please help. Thanks 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 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); <?php function time_convert($l_timestamp) { $l_timestamp = $l_timestamp * 86400; $time = date('Y-m-d',$l_timestamp); echo(date('Y-m-d',$l_timestamp)); //This does return (date('Y-m-d',$l_timestamp)); //This does not work return $time; // This does not work echo $time; //This does not work } time_convert(32155.0); ?> Can anyone help me, I am not getting the return parameter as I want 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. Hello People, I've been racking my brains, searching google and trawling through the many pages here to see if someone else has already asked the same question but I cant find it. I'm trying to build a simple redirect script based on the referring URL. I want to direct the user to "page a" if any of the arrays are found and "page b" if not. This is what I have so far Code: [Select] <?php $main_string="@$HTTP_REFERER"; $arr = array('aaaa','bbbb','cccc'); foreach($arr as $key => $search_needle) { if(stristr($main_string, $search_needle) != FALSE) { echo "<meta http-equiv=Refresh content=0;url=http://www.google.com/PAGEA.html>"; } } ?> I thought that it might be a case of adding the "else" command but doing that will always redirect to "page b" if all of the arrays aren't true and only 1 needs to be. Anyone got any ideas? Thanks I have 2 referring url.
USERNAME
http://testsite.com/...gister&ref=test
USER ID
http://testsite.com/...gister&ref_id=1
1. Code to remember ref and ref_id
2. Code to allow user to insert own referrer if no ref link detected?
3. Store ref and ref_id into same column Like db referrer = username/user_id or referrer = username / referrer_id = user_id ?
PHP
$ref_id = isset($_GET['ref_id']) ? filter_input(INPUT_GET, 'ref_id', FILTER_SANITIZE_STRING) : ''); $ref = isset($_GET['ref']) ? filter_input(INPUT_GET, 'ref', FILTER_SANITIZE_STRING) : '');FORM if(!empty($_GET['ref_id'])){ print ' <tr> <td style="font-weight:bold">Referred by user id #</td> <td><input type="text" name="ref_id" maxlength="255" style="width:200px" value="'.cleanOutput($ref_id).'"></td> </tr>'; }else{ print ' <tr> <td style="font-weight:bold">Referred by</td> <td><input type="text" name="ref" maxlength="255" style="width:200px" value="'.cleanOutput($ref).'"></td> </tr>'; }2. $ref = isset($_GET['ref']) ? filter_input(INPUT_GET, 'ref', FILTER_SANITIZE_STRING) : (isset($_POST['ref']) ? filter_input(INPUT_POST, 'ref', FILTER_SANITIZE_STRING) : ''); Hey! I am having issues with my site not going to the correct page after submitting a form to login or register or anything in that regards. The script that referrers the user is poster below: Code: [Select] <? /** * Process.php * * The Process class is meant to simplify the task of processing * user submitted forms, redirecting the user to the correct * pages if errors are found, or if form is successful, either * way. Also handles the logout procedure. * * Written by: Jpmaster77 a.k.a. The Grandmaster of C++ (GMC) * Last Updated: August 19, 2004 */ include("session.php"); class Process { /* Class constructor */ function Process(){ global $session; /* User submitted login form */ if(isset($_POST['sublogin'])){ $this->procLogin(); } /* User submitted registration form */ else if(isset($_POST['subjoin'])){ $this->procRegister(); } /* User submitted forgot password form */ else if(isset($_POST['subforgot'])){ $this->procForgotPass(); } /* User submitted edit account form */ else if(isset($_POST['subedit'])){ $this->procEditAccount(); } /** * The only other reason user should be directed here * is if he wants to logout, which means user is * logged in currently. */ else if($session->logged_in){ $this->procLogout(); } /** * Should not get here, which means user is viewing this page * by mistake and therefore is redirected. */ else{ header("Location: index.php"); } } /** * procLogin - Processes the user submitted login form, if errors * are found, the user is redirected to correct the information, * if not, the user is effectively logged in to the system. */ function procLogin(){ global $session, $form; /* Login attempt */ $retval = $session->login($_POST['user'], $_POST['pass'], isset($_POST['remember'])); /* Login successful */ if($retval){ header("Location: ".$session->referrer); } /* Login failed */ else{ $_SESSION['value_array'] = $_POST; $_SESSION['error_array'] = $form->getErrorArray(); header("Location: ".$session->referrer); } } /** * procLogout - Simply attempts to log the user out of the system * given that there is no logout form to process. */ function procLogout(){ global $session; $retval = $session->logout(); header("Location: logout.php"); } /** * procRegister - Processes the user submitted registration form, * if errors are found, the user is redirected to correct the * information, if not, the user is effectively registered with * the system and an email is (optionally) sent to the newly * created user. */ function procRegister(){ global $session, $form; /* Convert username to all lowercase (by option) */ if(ALL_LOWERCASE){ $_POST['user'] = strtolower($_POST['user']); } /* Registration attempt */ $retval = $session->register($_POST['user'], $_POST['pass'], $_POST['email']); /* Registration Successful */ if($retval == 0){ $_SESSION['reguname'] = $_POST['user']; $_SESSION['regsuccess'] = true; header("Location: ".$session->referrer); } /* Error found with form */ else if($retval == 1){ $_SESSION['value_array'] = $_POST; $_SESSION['error_array'] = $form->getErrorArray(); header("Location: ".$session->referrer); } /* Registration attempt failed */ else if($retval == 2){ $_SESSION['reguname'] = $_POST['user']; $_SESSION['regsuccess'] = false; header("Location: ".$session->referrer); } } /** * procForgotPass - Validates the given username then if * everything is fine, a new password is generated and * emailed to the address the user gave on sign up. */ function procForgotPass(){ global $database, $session, $mailer, $form; /* Username error checking */ $subuser = $_POST['user']; $field = "user"; //Use field name for username if(!$subuser || strlen($subuser = trim($subuser)) == 0){ $form->setError($field, "* Username not entered<br>"); } else{ /* Make sure username is in database */ $subuser = stripslashes($subuser); if(strlen($subuser) < 5 || strlen($subuser) > 30 || !eregi("^([0-9a-z])+$", $subuser) || (!$database->usernameTaken($subuser))){ $form->setError($field, "* Username does not exist<br>"); } } /* Errors exist, have user correct them */ if($form->num_errors > 0){ $_SESSION['value_array'] = $_POST; $_SESSION['error_array'] = $form->getErrorArray(); } /* Generate new password and email it to user */ else{ /* Generate new password */ $newpass = $session->generateRandStr(8); /* Get email of user */ $usrinf = $database->getUserInfo($subuser); $email = $usrinf['email']; /* Attempt to send the email with new password */ if($mailer->sendNewPass($subuser,$email,$newpass)){ /* Email sent, update database */ $database->updateUserField($subuser, "password", md5($newpass)); $_SESSION['forgotpass'] = true; } /* Email failure, do not change password */ else{ $_SESSION['forgotpass'] = false; } } header("Location: ".$session->referrer); } /** * procEditAccount - Attempts to edit the user's account * information, including the password, which must be verified * before a change is made. */ function procEditAccount(){ global $session, $form; /* Account edit attempt */ $retval = $session->editAccount($_POST['curpass'], $_POST['newpass'], $_POST['email']); /* Account edit successful */ if($retval){ $_SESSION['useredit'] = true; header("Location: ".$session->referrer); } /* Error found with form */ else{ $_SESSION['value_array'] = $_POST; $_SESSION['error_array'] = $form->getErrorArray(); header("Location: ".$session->referrer); } } }; /* Initialize process */ $process = new Process; ?> All it does is bring you to a unknown page. Thanks! I have a php code that call a process (a java program). The process runs in background for a so long time - from 1 minute to 2 hours. I make the process call using exec:
<?php function execInBackground($cmd) { if (substr(php_uname(), 0, 7) == "Windows"){ pclose(popen("start /B ". $cmd, "r")); } else { exec($cmd . " &> /dev/null &"); } } $command = "java ... ReadEmailsCmdLine 2>&1"; execInBackground($command); echo "FINALIZADO";Though the command runs in background it looks that the PHP code wait for the command completion to send a answer to client browser because I just see "FINALIZADO" after the java program finished its execution. Does someone know how I can solve this problem? Or at least does have a hint/pointer for that? Since now I thanks any comment/suggestion Write a function Counter() that keeps track of the number of times it is called. This function should not take any parameters and return the number of times that it has been called. $_SERVER['HTTP_REFERER']; <-- this returns the full path. Can I return the referring page name only? ie) somepage.php Cheers First, I would like to say when i tried to recover my account from this website, I took me 10 attempts to get the captcha right and then finally it said it sent me an email to my gmail account. I checked spam folder and everything there was no such email from this site. Then I decided to create a new account, well, it took me another 10 attempts to get the captcha right and finally when it was submitted, the page was loading for around 3 minutes before it signed me in.
My question is about the php date() function. It accepts a format to display a time. In the following example I use F for full representation of month, d for 2-digit day of month with leading zeros, Y for full year, g for 12-hour format without leading 0s, s for seconds and A for meridiem. It uses the correct format, but it gives me the wrong time. My local time is 4:43 and it prints out 4:12:
<?php echo "<p>order processed on " . date("F d, Y g:sA") . "</p>"; ?> Why is it 30 minutes behind? I wrote this bit of code to get the next six datetimes of a certain date. It was working perfectly until this week. Code: [Select] <?php $start = "2011-11-06"; $num_days = 6; for ($i = 0; $i <= $num_days; $i += 1) { $stamp = strtotime($start) + ($i * 86400); echo date('l - n/d/Y - h:i a',$stamp)."<br/>"; } ?> The 2011-11-06 is a Sunday so i would expect to get the following: Sunday - 11/06/2011 - 12:00 am Monday - 11/07/2011 - 12:00 am Tuesday - 11/08/2011 - 12:00 am Wednesday - 11/09/2011 - 12:00 am Thursday - 11/10/2011 - 12:00 am Friday - 11/11/2011 - 12:00 am Saturday - 11/12/2011 - 12:00 am instead it is producing: Sunday - 11/06/2011 - 12:00 am Sunday - 11/06/2011 - 11:00 pm Monday - 11/07/2011 - 11:00 pm Tuesday - 11/08/2011 - 11:00 pm Wednesday - 11/09/2011 - 11:00 pm Thursday - 11/10/2011 - 11:00 pm Friday - 11/11/2011 - 11:00 pm How should i go about fixing this so it doesn't happen again? |