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

x
?
Solved

random date format

Posted on 2011-05-06
5
Medium Priority
?
287 Views
Last Modified: 2012-05-11
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
Comment
Question by:rgb192
5 Comments
 

Expert Comment

by:doctorkt
ID: 35710313
Is this what you're looking for?
 
<? print(Date("l F d, Y")); ?>

Open in new window

0
 

Author Comment

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

Accepted Solution

by:
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)]);
}

Open in new window


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

Assisted Solution

by:Ray Paseur
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

Also this link will help you test for valid or invalid date strings:
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)))
    ;
}

Open in new window

0
 

Author Closing Comment

by:rgb192
ID: 35722158
thanks
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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

868 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question