[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
Solved

# random date format

Posted on 2011-05-06
Medium Priority
287 Views
Display today's date in a random format.  One format could be 4 digit year another 2 digit year.  I only want to echo one date
0
Question by:rgb192

Expert Comment

ID: 35710313
Is this what you're looking for?

``````<? print(Date("l F d, Y")); ?>
``````
0

Author Comment

ID: 35710917
That is one date.  I would like a random generator to pick other date formats
0

LVL 12

Accepted Solution

Mohamed Abowarda earned 1000 total points
ID: 35711142
I have made the following function that you can use:
``````function randomDate()
{
\$randomFormat[0] = "m/d/Y";
\$randomFormat[1] = "m-d-Y";
\$randomFormat[2] = "Y-m-d";
\$randomFormat[3] = "Y-m-d";
return date(\$randomFormat[mt_rand(0, sizeof(\$randomFormat) - 1)]);
}
``````

Change the items in the array '\$randomFormat' to date formats you want to allow.
0

LVL 111

Assisted Solution

Ray Paseur earned 1000 total points
ID: 35712302
Not sure why you would want to do this, but if you want more information about DATETIME processing, I have an article here at EE that might help you understand the many different formats of dates.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

http://www.laprbass.com/RAY_strtotime.php

Note that there are different meanings associated with the patterns when the / or - are used as separators.  The interpretation from the current timestamp into the date string is logically consistent, however the interpretation from the date string into the timestamp varies.  This may be more randomness than you wanted!
http://www.laprbass.com/RAY_temp_rgb192.php

Script outputs:
m/d/Y 05/07/2011 1,304,744,400
m-d-Y 05-07-2011 1,309,842,000
m-j-y 05-7-11 1,121,058,000
m/j/y 05/7/11 1,304,744,400
n/j/y 5/7/11 1,304,744,400
n-j-y 5-7-11 1,121,058,000
Y-m-d 2011-05-07 1,304,744,400
``````<?php // RAY_temp_rgb192.php
error_reporting(E_ALL);

// REQUIRED SETTING FOR PHP 5.1+
date_default_timezone_set('America/Chicago');

// MAKE OUTPUT EASY TO READ
echo "<pre>";

// MAN PAGE: http://php.net/manual/en/function.date.php
\$fs[] = "m/d/Y";
\$fs[] = "m-d-Y";
\$fs[] = "m-j-y";
\$fs[] = "m/j/y";
\$fs[] = "n/j/y";
\$fs[] = "n-j-y";
\$fs[] = "Y-m-d";

// SHOW SOME DIFFERENT DATES
foreach (\$fs as \$f)
{
echo PHP_EOL
. \$f
. ' '
. date(\$f)
. ' '
. number_format(strtotime(date(\$f)))
;
}
``````
0

Author Closing Comment

ID: 35722158
thanks
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
###### Suggested Courses
Course of the Month20 days, 7 hours left to enroll