PHP Date-Dependent Pagination - php

I have a site that stores a bunch of records in an sql database that i want to pull onto a page based on the date, which is stored as Y-m-d in sql. But, I want to paginate the results based on day.
So for the index, i want to show all the results from the current day, for which i was going to use the php date() function as the WHERE in my QUERY. But I'm hitting a snag on doing the pagination. I want to have buttons at the bottom that go to the next page with a get, so index.php?page=2 would be tomorrow, but i cant figure out how to select "tomorrow" reliably from the database in my WHERE.
See, i was going to use date("U") to get the unix time in seconds of the first day on the first page and then just add 3600*$_GET['page'] for incrementing the date on the next pages, but that seems like a sloppy way to do it that might wind up messing me up. Is this the only way or is there a better, more practical solution - thanks a lot guys I appreciate it.

If page 2 is tomorrow, then you're going to be looking at something like this:
$days_ahead = $page - 1;
$query = "... WHERE date = DATE(NOW()) + INTERVAL $days_ahead DAY ...";
Note that this would work fine on the first page too (assuming $page gets defaulted to 1), it'd add 0 days to today's date.

You experiment with strtotime:
$sqldate = date('Y-m-d', strtotime('+2 days'));

This is how I managed to fix it for my site
//'page' is a GET variable from url
if($page<=1) {
$datemax = time();
$datemin = time() - (1 * 2592000); //2592000 being seconds in a month
}
else{
$datemax = time() - (($page - 1) * 2592000);
$datemin = time() - ($page * 2592000);
}
And then obviously the query will look something like
SELECT * FROM posts WHERE dateposted >=$datemin AND dateposted <=$datemax

Related

Mysql subtime currenttime - $row['time']

Im building a simple web page. on loading the page the first time I store the current time in a database. When the page is loaded again I want to get the time that has past since the first load.
I figured out I probably have to use SUBTIME to achieve this. so basicly it would be something like this:
$result = mysql_query("SELECT SUBTIME('now()','time')"); // in this case time is the name of the database row.
I looked around on the net but cant find an example that explains me how to do this. Hope anyone can help me out.
Greetings!
If 'time' is the name of your database column use backticks instead of quotes to get the value:
$result = mysql_query("SELECT SUBTIME(NOW(),`time`) AS `total_time`");
NOW() should not be in quotes.
I would not use MySQL for this.
retrieve the time from your database
$starttime = strtotime($row['time']); // hopefully a datetime value
$endtime = time();
$totaltime = $endtime - $starttime; // gives the time in seconds
and then write it back into your database table, or whatever you want to do with that info

Retrieve rows within current month, week, and day using php and mysql

I have a mysql table events with column event_date varchar(10) which takes in dates in the form 7-5-2014.
I have an object which can give me:
// displays current month (5)
$m = $time->getMonth();
echo $m;
//displays current day (1)
$d = $time->getDay();
echo $d;
// displays current year (2014)
$y = $time->getYear();
echo $y;
I am trying to figure out a way to get events within the current day, month, and year.
The php query below is wrong but it gives you and idea as to what im trying to do:
$eventsWithinDay = query("SELECT * FROM events WHERE LEFT(event_date, 2)='%s'", $d);
How can I do this correctly?
Better yet, what is the most efficient way to do this?
I can make any necessary changes to the database or php.
Thanks in advance.
You an do it in various ways. For instance:
SELECT smth FROM somewhere WHERE datefield LIKE '2014-05-%'
OR
SELECT smth FROM somewhere WHERE datefield >= $start_interval and date <= $end_interval
In the latter case you'd have your date stored in a BIGINT column and use php's time() to set its value. Then using PHP's time functions (like strtotime('-1 day')) get timestamps for dates you wish to query against. So a simple example would be:
$q = "SELECT ... WHERE datefield >= " . strtotime('-2 days') // this would search for dates from at least 2 days ago
You could also use MYSQL's built-in date-time functions, but i have no experience with those so i'm not going to advice on something i don't know myself :)

Updating 'Date' Data based on Current Date

Let's say I have a table and it consists a column of next_update (which is in a date format), time_left (which is in the unit of days). How could I program it for example the next_update is 27/02/14 and the time_left is 3 days for today(24/02/14) view in a php webpage but for tomorrow view in the php page will be automatically deducted to 2 days. I'm using postgresql as my database and php as the web interface. The main problem now is how can I make the value of time_left be minus by the next_update with the current date.
I've gone through some basic manual but still have no idea to set this up. Sincerely thank you all for any help.
Try this code
<?php
$now = time();
$next_update = strtotime("2014-02-27");
$datedifferent = $next_update - $now;
$days = floor($datedifferent/(60*60*24));
if($days > 0) {
echo $days.' more days you have';
}
?>
SELECT next_update, next_update - now() as time_left FROM <your_table>
You may also want to apply function EXTRACT to subtract necessary time units from interval (documentation)

Why isnt my MySQL BETWEEN operator not working?

MySQL table "flightSched" is connected to time, similar to the one below:
flightNo |day |time |arrivalTimeSec
=============================================
WERE112 |Tuesday | 1:00 |1381186800
FGHG234 |Tuesday |23:00 |1381266000
CGHF345 |Tuesday |00:00 |1381183200
I have a mysql query that select all data between two times. This is the query:
$CurrentTimeMinus30min = date('H:i', strtotime('-30 minutes')); //Current Time minus 30minutes
$CurrentTimeMinus30min = strtotime($CurrentTimeMinus30min);
$CurrentTimePlus4Hours = date('H:i', strtotime('+240 minutes')); //Current Time plus 4 hours
$CurrentTimePlus4Hours = strtotime($CurrentTimePlus4Hours);
$query = $mysqli->query("
SELECT * FROM flightSched
WHERE day = '$currentDay'
AND arrivalTimeSec
BETWEEN '$CurrentTimeMinus30min'
AND '$CurrentTimePlus4Hours'
");
I was advised to used strtotime() function on the time values to be able to use them in a BETWEEN MySQL query. This doesn't seem to be working at all.
Where am I going wrong with this query? Any help will be appreciated.
today I found the same problem with yours (mine about coordinates).
and I found out that in some case, a BETWEEN operator can only be used like this
..... WHERE columname BETWEEN smallervalue AND biggervalue
previously I've tried with the biggervalue at front since I dealt with negative numbers, and it fails.
you might found the same problem with your timestamp.
strtotime returns a timestamp so passing that into the MySQL query, like above, won't work. Try using FROM_UNIXTIME instead.
$query = $mysqli->query("SELECT * FROM flightSched
WHERE day = '$currentDay'
AND FROM_UNIXTIME(arrivalTimeSec) BETWEEN FROM_UNIXTIME($CurrentTimeMinus30min) AND FROM_UNIXTIME($CurrentTimePlus4Hours) " );
EDIT - I hadn't noticed that arrivalTimeSec was also a timestamp. The above mightn't be a workable answer for you, but try it. If it doesn't work, as others say, define what you mean by
This doesn't seem to be working at all.
Is it not returning any rows? Is it returning an error? Can you print out $CurrentTimeMinus30min and $CurrentTimePlus4Hours? Narrow down the potential areas for problems.
Have you tried to encapsulate the between? This could potentially solve your problem:
SELECT * FROM flightSched
WHERE day = '$currentDay'
AND (arrivalTimeSec BETWEEN '$CurrentTimeMinus30min' AND '$CurrentTimePlus4Hours')
Also why not just do:
$CurrentTimeMinus30min = strtotime('-30 minutes');
Or
$CurrentTimeMinus30min = strtotime(date('Y-m-d H:i:00', strtotime('-30 minutes')));
Please send us some examples of what your variables are generating.
Your time calculation with date("H:i",...) and strtotime(..) seems to actually produce the correct results, although there is a much easier way to add/substract n minutes from the current time:
$now = time();
$currentTimeMinus30min = $now - 30*60; // 30 minutes * 60 seconds
$currentTimePlus4Hours = $now + 4*60*60; // 4 hours * 60 minutes * 60 seconds
(I assume your time entries in your database are unix timestamps.)
Your query looks fine, too, but there are a few things to keep in mind:
You have redundant fields in your database (day and time can be calculated from the timestamp)
Working with time variables can easily lead to confusion, as the time passes on and if you have no entries in your database that match the specified time range (-30m to +240m) the result set is empty. So to test the query update the database with current time stamps.
I would suggest the following:
Drop the redundant columns day and time and just use the timestamp as base for your calculations, because the day and time is already included in the timestamp. So just use a simple query like
select * from flightShed
where arrivalTime between $begin and $end

Time difference between current time and timestamp+milliseconds

I'm wanting to display on a php page the difference between the current server time and a datetime row plus a row that has milliseconds in it, so I guess the equation would look kind of like ((Datetime+Milliseconds)-Server Time).
The only problem is, I'm not sure how to do it in code. I can currently get the difference between the datetime row and the current time with echo strtotime($row['date_added']) - time(); When I try adding the row that contains the milliseconds, date_mil, I get a really long number.
The date in the row date_added looks like 2012-05-25 16:55:06 and the value of the date_mil is around 218238.
I'm still learning how to do all of this, and this has me confused. Thanks for the help!
I just solved my own problem.
$difference = time() - strtotime($row['date_added']);
$milliseconds = round(($row['date_mil']) * .001);
echo $milliseconds - $difference;

Categories