Solved

PHP Change Numeric Date to Text Date

Posted on 2009-03-31
10
1,263 Views
Last Modified: 2013-12-13
Hello,
I'm trying to convert the date "20090328" into "March 28th, 2009" with PHP and getting a little stuck. I need this asap and don't have much time. Can someone fix up this code for me? Thanks!
<?php

		$file = "aaaaaaaaaa20090329aaaa";

		$file_date = substr($file, 10, -4); //get the date from the file name

		$file_year = substr($file_date, 0, -4); //get the year from the file date

		$file_month = substr($file_date, 4, -2); //get the month from the file date

		$file_day = substr($file_date, 6); //get the day (numeric) of the file

		$constructed_date = "$file_month/$file_day/$file_year";

		print($constructed_date);

?>

Open in new window

0
Comment
Question by:damijim
  • 6
  • 4
10 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 24031548
use strtotime and date function to do this.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 24031599

<?php // RAY_temp_strtotime.php
 

// TEST DATA

$file = "aaaaaaaaaa20090329aaaa";
 

// GET DATE THING OUT OF STRING BY ANY CONVENIENT MEANS

$date = str_replace('a', '', $file);
 

// GET TIMESTAMP

$ts = strtotime($date);
 

// GET PRINTABLE DATE

$date_printable = date('m/d/y', $ts);

print_r($date_printable);

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 24031639
Oops, sorry - need better date formatting.  Try this instead...
<?php // RAY_temp_strtotime.php
 

// TEST DATA

$file = "aaaaaaaaaa20090329aaaa";
 

// GET DATE THING OUT OF STRING BY ANY CONVENIENT MEANS

$date = str_replace('a', '', $file);
 

// GET TIMESTAMP

$ts = strtotime($date);
 

// GET FORMATTED DATE

$date_formatted = date('F jS, Y', $ts);

print_r($date_formatted);

Open in new window

0
 
LVL 1

Author Comment

by:damijim
ID: 24031675
Well, I tried putting that code in to mine, and I end up with December 31, 1969 ...

the string passed was "aaaaaaaaaa20090329aaaa"
$file_date = substr($file, 10, -4); //get the date from the file name

$file_year = substr($file_date, 0, -4); //get the year from the file date

$file_month = substr($file_date, 4, -2); //get the month from the file date

$file_day = substr($file_date, 6); //get the day (numeric) of the file

$constructed_date = "$file_month$file_day$file_year";
 

// GET TIMESTAMP

$ts = strtotime($constructed_date);

// GET PRINTABLE DATE

$date_printable = date('F j, Y', $ts);

print_r($date_printable);

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 24031687
Print out the $constructed_date and post it here, please.

What version of PHP are you using?

Thanks, ~Ray
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:damijim
ID: 24031711
PHP v5.2.3

output of $construct_date = 03302009

agh, my mistake, I can easily reorder the $construct_date variables if it's easier..
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 24031731
Yes, - the strtotime() function can deal with YYYYMMDD format (which is ISO8601-format) so it's good to use that for all internal representations of the date.  

Best regards, ~Ray
0
 
LVL 1

Author Comment

by:damijim
ID: 24031751
Awesome, I switched it to:

$constructed_date = "$file_year$file_month$file_day";

and it's out putting what I want... Thanks!!
0
 
LVL 1

Author Closing Comment

by:damijim
ID: 31564949
Thanks again!
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 24031763
Glad to help, and thanks for the points.  You  can test any date string on my web site here:

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

Best always, ~Ray
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

947 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now