Jump to content

PHP Organize Date Variables

dirtbikerxz
Go to solution Solved by mariushm,

Put them in an array  as datetime objects or maybe build a string in the form  YYYYMMDD  (add zeroes where necessary).  With strings in each array

If you use strings, you can do a simple sort. With datetime objects, you can create a custom sort function :
 

<?php

$the_array = array();
$the_array[] = [1, new Datetime('2016-04-30')]; // year , month , day
$the_array[] = [2, new Datetime('2016-03-25')]; // year , month , day

// add the others

$the_array[] = [5,new Datetime('2016-04-12')]; // year , month , day

// show the contents of the array
var_dump($the_array);

// sort the array using a callback function

function compare_array_entries($a, $b) {

// $a = one entry in array, $b = following entry in array
// [0] = the number , [1] = the datetime object

  if ($a[1] == $b[1]) {
    return 0;
  }

  return ($a[1]< $b[1]) ? -1 : 1;
}

// now do the actual sorting using the callback function above to actually compare two entries at a time

usort($the_array,'compare_array_entries');

// dump the array contents again

var_dump($the_array);

// now you can use something like foreach ($the_array as $key = > $value)  {   }  to go through the array. , $value[0] is your number, $value[1] is your datetime object

?>

 

 

So I have 5 variables containing dates (in php)

 

$regend1 =  2016-04-30

$regend2 =  2016-03-25

$regend3 =  2016-04-12

$regend4 =  2016-04-20

$regend5 =  2016-02-07

 

How can i order them from lowest to highest using php? The output needs to be something that says $regend5 is first, $regend2 is second, $regend3 is third, $regend4 is fourth, and $regend1 is fifth ... or something like that. Thanks in advance

Link to comment
Share on other sites

Link to post
Share on other sites

Put them in an array  as datetime objects or maybe build a string in the form  YYYYMMDD  (add zeroes where necessary).  With strings in each array

If you use strings, you can do a simple sort. With datetime objects, you can create a custom sort function :
 

<?php

$the_array = array();
$the_array[] = [1, new Datetime('2016-04-30')]; // year , month , day
$the_array[] = [2, new Datetime('2016-03-25')]; // year , month , day

// add the others

$the_array[] = [5,new Datetime('2016-04-12')]; // year , month , day

// show the contents of the array
var_dump($the_array);

// sort the array using a callback function

function compare_array_entries($a, $b) {

// $a = one entry in array, $b = following entry in array
// [0] = the number , [1] = the datetime object

  if ($a[1] == $b[1]) {
    return 0;
  }

  return ($a[1]< $b[1]) ? -1 : 1;
}

// now do the actual sorting using the callback function above to actually compare two entries at a time

usort($the_array,'compare_array_entries');

// dump the array contents again

var_dump($the_array);

// now you can use something like foreach ($the_array as $key = > $value)  {   }  to go through the array. , $value[0] is your number, $value[1] is your datetime object

?>

 

 
Link to comment
Share on other sites

Link to post
Share on other sites

Thanks! Will try it out as soon as I get home and let you know. 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×