Jump to content

[SOLVED] Sorting dates (only DDMM) from the current day (DDMM)


Recommended Posts

Hi.

The last two days I have searched both PHP and MySQL stuff to help my script but have decided to try using a forum and this one seems the best!

I am writing a script that shows anniversaries (birthdays, wedding, other) in order of time left until that anniversary. So the year the anniversary happened does not matter. For example I was born 10.9.1982 but i am just after "8 months, 16 days till anniversary" for example.

I have written a function that calculates the "8 months, 16 days" bit for echo'ing (although it needs fixing I think I am ontop of that one).  The function takes the arguments $d and $m (DD and MM) for the current anniversary in the foreach loop.

I have fetched all the anniversaries I interested in from the MySQL table p1_anni and used ORDER BY month, day. Therefore the order is from the start of the calander year 1st Jan, to 31st Dec. (I have tried to do clever things with the MySQL ORDER BY clause to return the array in the order I am after, but no luck. NB - using ORDER BY CURDATE() => date or the such like only gives values for the rest of the year.)

So I figure I should use PHP to resort the array that is from 1st Jan, to 31st Dec. I thought I would split it in two, using the current day as the location (or the next entry in the array after the current day) and then join the two arrays back together in the other order so that they are in the order I am after.

use: array_merge & array_slice
also need some PHP functions to give as parameters for array_slice (offset etc).

I don't expect anyone to write a script for me. Rather I am asking for tips / advice as to how to takle this problem.

(writing this has just made me think: maybe i could use two MySQL SELECT queries, with a UNION, that finds the dates to come and the dates gone in the calander year.... will give that a go now. Any ideas greatly appreciated!)

Rob



My advice would be to use strtotime() to get the 'seconds from the epoch' value for the particular day of the year and then order it by that.

You'll need to include the current year in that code.

Regards
Huggie
The following code works, but 'everything' is being processed in PHP.  If you refined your query, you could reduce a lot of this processing.

[code]<?php

// Get the current 'timestamp'
$today = strtotime('today');

// Array of test dates
$dates = array('0601','1406','0809','1912');

// Get current year
$yyyy = date("Y", $today);

// Loop through putting the results in an array keyed on timestamp
foreach($dates as $d){
  $dd = substr($d, 0, 2); // get dd
  $mm = substr($d, 2, 2); // get mm
  $date = $yyyy.$mm.$dd; // join date with year
  $ds = strtotime($date);
  if ($ds < $today){ // if the date has past this year, then show next years
      $nyyyy = $yyyy+1;
      $date = $nyyyy.$mm.$dd;
      $ts = strtotime($date);
      $date_array[$ts] = $d; // put date into array
  }
  else { // if date hasn't past
      $ts = strtotime($date);
      $date_array[$ts] = $d; // put date into array
  }
}

// Sort ascending
ksort($date_array);

// Dump the array to prove it works
echo "<pre>\n";
print_r($date_array);
echo "</pre>\n";

?>[/code]

In the above example, it takes the date that you've provided in DDMM format and returns the time stamp for that date (in the current year) if that date's passed, then it gets the timestamp for that date next year.

It puts these values into an array keyed on time stamp and then orders them.

Regards
Huggie
Cheers Huggie!

I have had a look at your code although yet to try it out. I have had to do other things the last two days but had an idea as to how I could sort the data with MySQL and just got that to work. Thanks for your code though - I am going to use some of the stuff in it for another purpose!

Here is the MySQL statement I used - a complete hash I think but it works. One day it would be nice to make it TIDY!

<?php
$query = "(SELECT 1 AS sortval, anni_type,anni_contact_ids,anni_d,anni_m,anni_y,anni_r1d,anni_r2d,anni_r1w,anni_r2w,anni_r3w,anni_other_title
FROM p1_anni
WHERE anni_m = MONTH(CURDATE()) AND anni_d >= DAY(CURDATE())
)
UNION ALL
(SELECT 2 AS sortval, anni_type,anni_contact_ids,anni_d,anni_m,anni_y,anni_r1d,anni_r2d,anni_r1w,anni_r2w,anni_r3w,anni_other_title
FROM p1_anni
WHERE anni_m > MONTH(CURDATE())
)
UNION ALL
(SELECT 3 AS sortval, anni_type,anni_contact_ids,anni_d,anni_m,anni_y,anni_r1d,anni_r2d,anni_r1w,anni_r2w,anni_r3w,anni_other_title
FROM p1_anni
WHERE anni_m < MONTH(CURDATE())
)
UNION ALL
(SELECT 4 AS sortval, anni_type,anni_contact_ids,anni_d,anni_m,anni_y,anni_r1d,anni_r2d,anni_r1w,anni_r2w,anni_r3w,anni_other_title
FROM p1_anni
WHERE anni_m = MONTH(CURDATE()) AND anni_d < DAY(CURDATE())
)
ORDER BY sortval,anni_m,anni_d";
?>

Cheers Huggie, Go well, Rob

PS - going mad - cannot see how to show this thread as solved... pants
[quote author=robr link=topic=123803.msg514695#msg514695 date=1169897999]
I forgot to say that I was after a solution that does not worry about the year as I want to use dates before the UNIX 1970 date.
[/quote]

My solution only needs the current year, so that will never have been a problem, unless we have another 1970  ;)

Huggie
But I thought the function strtotime() gave the seconds from 1.1.1970....

From PHP.net:
Note:  The valid range of a timestamp is typically from Fri, 13 Dec 1901 20:45:54 GMT to Tue, 19 Jan 2038 03:14:07 GMT. (These are the dates that correspond to the minimum and maximum values for a 32-bit signed integer.) Additionally, not all platforms support negative timestamps, therefore your date range may be limited to no earlier than the Unix epoch. This means that e.g. dates prior to Jan 1, 1970 will not work on Windows, some Linux distributions, and a few other operating systems. PHP 5.1.0 and newer versions overcome this limitation though.

Anyway, am still new to all this, cheers once again!
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.