brentman Posted February 21, 2015 Share Posted February 21, 2015 I have users who signed up for an email list and there is a table of them and the join date is in unix timestamp. I do not care about the date, only the time they signed up. I am putting together code to be run on cron to decide who gets emailed. It will be run probably every hour. When it runs the first thing I need it to do is select all users who from the current time through the next hour registered. This way I can then email people around the time they initially joined my site. Here is what I have: //$input is the minutes range (which will be most likely an hour so this variable should be 60) function to_email($input) { require_once('dbconnect.php'); $cur_hours = date('H'); $cur_minutes = date('i'); $cur_seconds = ($cur_minutes + ($cur_hours * 60)) * 60; $end_seconds = $input * 60; $end_seconds = $cur_seconds + $end_seconds; $result = mysql_query("SELECT * FROM usertable WHERE " . $cur_seconds . " <= (HOUR(opt_time)*60*60) + (MINUTE(opt_time)*60) + SECOND(opt_time) AND '" . $end_seconds . " > (HOUR(opt_time)*60*60) + (MINUTE(opt_time)*60) + SECOND(opt_time)'"); $row = mysql_fetch_array($result); } The php part works as what I think is needed. But the query does not. I am not very good at MySQL queries so don't judge. Quote Link to comment https://forums.phpfreaks.com/topic/294787-getting-just-seconds-out-of-a-unix-timestamp/ Share on other sites More sharing options...
Solution Barand Posted February 21, 2015 Solution Share Posted February 21, 2015 Seems easier to select those where the current hour matches the hour they signed up SELECT ... WHERE HOUR(NOW()) = HOUR(FROM_UNIXTIME(opt_time)) Quote Link to comment https://forums.phpfreaks.com/topic/294787-getting-just-seconds-out-of-a-unix-timestamp/#findComment-1506367 Share on other sites More sharing options...
brentman Posted February 21, 2015 Author Share Posted February 21, 2015 Well that looks pretty easy, I like it. But what happens if I need only 30 minutes? What about 2 hours? I am not quite sure of how often the cron job will run yet. Quote Link to comment https://forums.phpfreaks.com/topic/294787-getting-just-seconds-out-of-a-unix-timestamp/#findComment-1506369 Share on other sites More sharing options...
brentman Posted February 21, 2015 Author Share Posted February 21, 2015 Also btw that query didn't work. Quote Link to comment https://forums.phpfreaks.com/topic/294787-getting-just-seconds-out-of-a-unix-timestamp/#findComment-1506370 Share on other sites More sharing options...
Barand Posted February 21, 2015 Share Posted February 21, 2015 Well no, you have to replace the "..." Quote Link to comment https://forums.phpfreaks.com/topic/294787-getting-just-seconds-out-of-a-unix-timestamp/#findComment-1506371 Share on other sites More sharing options...
brentman Posted February 21, 2015 Author Share Posted February 21, 2015 (edited) function to_email($input) { require_once('dbconnect.php'); echo time(); $result = mysql_query("SELECT * FROM usertable WHERE HOUR(NOW()) = HOUR(FROM_UNIXTIME(opt_time))"); $row = mysql_fetch_array($result); echo mysql_num_rows($row); } usertable one records opt_time value is 1424557192 I ran the script and it echoed: 1424557019 And this error: Warning: mysql_num_rows() expects parameter 1 to be resource, array given in /home/mysite/public_html/api/index.php on line 45 Edited February 21, 2015 by brentman Quote Link to comment https://forums.phpfreaks.com/topic/294787-getting-just-seconds-out-of-a-unix-timestamp/#findComment-1506372 Share on other sites More sharing options...
Barand Posted February 21, 2015 Share Posted February 21, 2015 It was working when it left the shop EG mysql> SELECT * FROM test_time; +------------+ | time | +------------+ | 1424476800 | | 1424480400 | | 1424484000 | | 1424487600 | | 1424491200 | | 1424494800 | | 1424498400 | | 1424502000 | | 1424505600 | | 1424509200 | | 1424512800 | | 1424516400 | | 1424520000 | | 1424523600 | | 1424527200 | | 1424530800 | | 1424534400 | | 1424538000 | | 1424541600 | | 1424545200 | | 1424548800 | | 1424552400 | | 1424556000 | | 1424559600 | +------------+ mysql> SELECT -> time -> , FROM_UNIXTIME(time) -> FROM test_time -> WHERE HOUR(NOW()) = HOUR(FROM_UNIXTIME(time)); +------------+---------------------+ | time | FROM_UNIXTIME(time) | +------------+---------------------+ | 1424556000 | 2015-02-21 22:00:00 | +------------+---------------------+ Quote Link to comment https://forums.phpfreaks.com/topic/294787-getting-just-seconds-out-of-a-unix-timestamp/#findComment-1506374 Share on other sites More sharing options...
Barand Posted February 21, 2015 Share Posted February 21, 2015 (edited) in your code echo mysql_num_rows($row); should be echo mysql_num_rows($result); Edited February 21, 2015 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/294787-getting-just-seconds-out-of-a-unix-timestamp/#findComment-1506375 Share on other sites More sharing options...
brentman Posted February 21, 2015 Author Share Posted February 21, 2015 Just did that right when I saw that ugh *facepalm*. Hourly is close enough for now. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/294787-getting-just-seconds-out-of-a-unix-timestamp/#findComment-1506376 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.