gaza165 Posted September 23, 2010 Share Posted September 23, 2010 This is probably a really easy question say i have timestamps like this Date: 1285372800 Date: 1285977600 Date: 1286582400 How can i find out and assign to a variable which of those is on or closest to todays date?? Quote Link to comment https://forums.phpfreaks.com/topic/214203-date-question/ Share on other sites More sharing options...
gaza165 Posted September 23, 2010 Author Share Posted September 23, 2010 Maybe if i post the code im doing you can better understand. <?php $feed = file_get_contents("events.xml"); $xml = new SimpleXmlElement($feed); $time = time(); foreach ($xml->channel->item as $entry){ $dc = $entry->children("http://purl.org/dc/elements/1.1/"); $geo = $entry->children("http://www.w3.org/2003/01/geo/wgs84_pos#"); echo "Title: ".$entry->title."</br>"; //Dates that event is on. foreach($dc as $date) { echo "Date: ".strtotime($date)."<br/>"; } //Geo Location - Latitude and Londitude echo "Lat: ".$geo[0]."<br/>"; echo "Long: ".$geo[1]."<br/>"; echo "------------------------------------------------------------<br/>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/214203-date-question/#findComment-1114548 Share on other sites More sharing options...
PFMaBiSmAd Posted September 23, 2010 Share Posted September 23, 2010 To find the closest to a value, you take the absolute value of the difference between the current value and the values in question. If you order the results of that in ascending order, the smallest value will be the same or closest to the current value. Untested, but should work - SELECT *, ABS(UNIX_TIMESTAMP() - your_date_column) as diff FROM your_table ORDER BY diff LIMIT 1 Edit: If your columns are actually a DATE or DATETIME, instead of the unix timestamp you showed in your first post, you would alter the above query to use the TIMEDIFF() function and either NOW() or CURDATE() inside of the ABS() function. Quote Link to comment https://forums.phpfreaks.com/topic/214203-date-question/#findComment-1114551 Share on other sites More sharing options...
gaza165 Posted September 23, 2010 Author Share Posted September 23, 2010 Sorry i should have explained better...I am not bringing the dates back from a database but bringing them back from an xml file Quote Link to comment https://forums.phpfreaks.com/topic/214203-date-question/#findComment-1114552 Share on other sites More sharing options...
PFMaBiSmAd Posted September 23, 2010 Share Posted September 23, 2010 The same method can be performed using php code. Quote Link to comment https://forums.phpfreaks.com/topic/214203-date-question/#findComment-1114555 Share on other sites More sharing options...
gaza165 Posted September 23, 2010 Author Share Posted September 23, 2010 What im trying to do is this... I have an XML File and i want to sort the events out by date So like 26th Septemeer List of all the events that have this date in their <dc:date> field. 28th September List of all the events that have this date in their <dc:date> field. XML FILE <?xml version="1.0" encoding="UTF-8"?> <rss version='2.0' xmlns:dc='http://purl.org/dc/elements/1.1/' xmlns:xCal="urn:ietf:params:xml:ns:xcal" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" > <channel> <item> <title><![CDATA[15 Feb 2010 - 10 Oct 2010: Funk, Soul, Disco, Hiphop]]></title> <geo:lat>51.74713</geo:lat><geo:long>-1.23755</geo:long> <dc:date>2010-09-25T01:00:00+01:00</dc:date> <dc:date>2010-10-02T01:00:00+01:00</dc:date> <dc:date>2010-10-09T01:00:00+01:00</dc:date> </item> <item> <title><![CDATA[26 Feb 2010 - 26 Oct 2010: Get Down]]></title> <geo:lat>51.74707</geo:lat><geo:long>-1.2351</geo:long> <dc:date>2010-09-24T01:00:00+01:00</dc:date> <dc:date>2010-10-01T01:00:00+01:00</dc:date> <dc:date>2010-10-08T01:00:00+01:00</dc:date> <dc:date>2010-10-15T01:00:00+01:00</dc:date> <dc:date>2010-10-22T01:00:00+01:00</dc:date> </item> </channel> </rss> Quote Link to comment https://forums.phpfreaks.com/topic/214203-date-question/#findComment-1114560 Share on other sites More sharing options...
salathe Posted September 23, 2010 Share Posted September 23, 2010 How does your last post relate to the original question? Quote Link to comment https://forums.phpfreaks.com/topic/214203-date-question/#findComment-1114566 Share on other sites More sharing options...
gaza165 Posted September 23, 2010 Author Share Posted September 23, 2010 Well it doesnt really but I was trying to figure out a way to get the sort of thing im looking for. Quote Link to comment https://forums.phpfreaks.com/topic/214203-date-question/#findComment-1114567 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.