Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8787039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:47:07+00:00 2026-06-13T21:47:07+00:00

How do you go about listing random dates (from earliest to newest)? I’ve been

  • 0

How do you go about listing random dates (from earliest to newest)?

I’ve been trying to accomplish this with PHP but with no success. I can get a random date to show up but keeps looping the same one over and over again rather than creating new ones and listing them in the order specified.

This is my code so far:

// Create a random date between 2 months
$datestart = strtotime('01-11-2012');
$dateend = strtotime('01-12-2012');
$daystep = 86400;
$datebetween = abs(($dateend - $datestart) / $daystep);
$randomday = rand(0, $datebetween);

for($i=0; $i< rand(10, 30) ;++$i)
{
echo "<div>" . date("d/m/Y", $datestart + ($randomday * $daystep)) ."</div>";
}

Update: I’ve now managed to get the code working thanks to Mihai Iorga but there is still the issue of sorting the dates from oldest to newest. How do I achieve this:

// Create a random date between 2 months
$datestart = strtotime('01-11-2012');
$dateend = strtotime('01-12-2012');
$daystep = 86400;
$datebetween = abs(($dateend - $datestart) / $daystep);
$randomday = rand(0, $datebetween);

for($i=0; $i< rand(10, 30) ;++$i)
{
$randomday = rand(0, $datebetween);
echo "<div>" . date("d/m/Y", $datestart + ($randomday * $daystep)) ."</div>";
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T21:47:09+00:00Added an answer on June 13, 2026 at 9:47 pm
    $datestart = strtotime('01-11-2012');
    $dateend = strtotime('01-12-2012');
    $daystep = 86400;
    $datebetween = abs(($dateend - $datestart) / $daystep);
    $dateArray[] = date("d/m/Y");
    $randomday = rand(0, $datebetween);
    
    for($i=0; $i< rand(10, 30) ;++$i)
    {
         $randomday = rand(0, $datebetween);
         $randomdate = date("d/m/Y", $datestart + ($randomday * $daystep));
         $dateArray[] = $randomdate;
    }
    
    sort($dateArray);
    
    foreach ($dateArray as $d) 
    { 
       echo "<div>" . $d ."</div>";
    } 
    

    Tested code on http://writecodeonline.com/php/ and running this outputs:

    05/11/2012
    06/11/2012
    07/11/2012
    08/11/2012
    08/11/2012
    12/11/2012
    12/11/2012
    12/11/2012
    19/11/2012
    20/11/2012
    24/11/2012
    27/11/2012
    28/11/2012
    30/11/2012
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to randomly grab about 20 random rows from a table, then using
This Question asked about listing directories (not files) in the current directory. I noted
I'm about to post my answer to this, as well, but I couldn't find
About six years ago, a software engineer named Harri Porten wrote this article ,
I'm trying to get a directory listing of files on an ftp server using
I don't really know that much about bash scripts OR imagemagick, but I am
In another question - Getting directory listing from SVN for use in ANT dropdown
This is a simple form for submitting a listing into a database of real
I had asked one question about this error before ( Android StackOverflowError in ViewGroup.resetResolvedTextDirection
In this question Alan posted links to interesting pages about PowerShell. I found also

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.