Solved

PHP Change Numeric Date to Text Date

Posted on 2009-03-31
10
1,262 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
Comment Utility
use strtotime and date function to do this.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility

<?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
Comment Utility
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
Comment Utility
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
Comment Utility
Print out the $constructed_date and post it here, please.

What version of PHP are you using?

Thanks, ~Ray
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Author Comment

by:damijim
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks again!
0
 
LVL 108

Expert Comment

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
The viewer will learn how to count occurrences of each item in an array.

762 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

15 Experts available now in Live!

Get 1:1 Help Now