Forum Moderators: coopster

Message Too Old, No Replies

Convert "Previous" Date(Ymd) to Date(yz) format?

Convert non-current Date format

         

fengshui

5:39 pm on Aug 21, 2008 (gmt 0)

10+ Year Member



I have some dates in a database that were stored using Date(Ymd) i.e. YYYYMMDD format. I just took over this database so wasn't part of the original design. Is there a way to take an "OLD" date stored in Ymd format and make a variable equal to the same date in yz format (essentially Julian date: YYDDD where DDD is a decimal representation of the day of the year)?

Any help in this would be appreciated before I write a many line routine that has to break the original date into three sections then determine the day of the year (taking into account leap years).

I'm sure (crossing fingers) there is a way to use built in PHP functions to do this in a couple of steps !

Thanks !
- Dan

cameraman

6:25 pm on Aug 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Have a look at date() [us2.php.net] and strtotime() [us2.php.net].

fengshui

6:59 pm on Aug 21, 2008 (gmt 0)

10+ Year Member



I've looked at those and haven't seen any format for either that allows you to have the function evaluate a string (previous date) and convert it. For example....

I pull a date from the data base into $date. (format Ymd i.e. YYYYMMDD). I want to convert it to a Julian date format yz (YYDDD where DDD is the day of the year and YY is the last two digits of the actual year).

You can assign a variable to the absolute current date/time using the date() function....which is how they got that into the database in the first place: $today = date(Ymd);

I would like to be able to take the $date from the database (in format YYYYMMDD) and make $exp equal to the julian date (equivalent of if during the original timestamp, date(yz);

- Dan

cameraman

8:41 pm on Aug 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$old = strtotime($date); // Convert database yyyymmdd to unix timestamp
$yr = date('y',$old); // Get 2 digit year from timestamp
$zday = date('z',$old); // Get day of year from timestamp

$exp = $yr . sprintf("%03d",$zday); // add leading zeroes to day

fengshui

10:03 pm on Aug 21, 2008 (gmt 0)

10+ Year Member



Thank You....I'm going to give this a go and get back to you about the results (and archive the technique for future endeavors); I failed to mention I'm an absolute neophyte in PHP (about 2-3 months experience so far) but took on a project that required more than that so every learning experience for me is pure gold at this point !

- Dan

cameraman

10:25 pm on Aug 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Cool, projects are the best way to learn. More hair loss maybe, but things sink in better (IMO).

fengshui

1:12 am on Aug 22, 2008 (gmt 0)

10+ Year Member



That worked (almost) perfectly...."good enough for the gals we go with anyways !". Need to add 2 after Feb 29 this year and add 1 all other years to make it work perfectly...I guess the PHP developers' functions didn't take that stuff into account.

THANK YOU THANK YOU THANK YOU....and this is excellent learned material to apply to other conversions when and if they come up ! now that I know how to do them !

- Dan (Fengshui)

g1smd

12:29 am on Aug 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Not sure why you call that "Julian Date". It's "Ordinal Day of Year".

Additionally, beware to NOT use two digits for year ever again.

fengshui

12:40 am on Aug 24, 2008 (gmt 0)

10+ Year Member



I'm retired Navy and Julian Date is an old military term. Generally its the last digit of the year appended directly to the day of the year i.e.....January 1 of this year would have been Julian date 8001.

Since you only get 4 digits for year or last 2 using date()...I went ahead and chose the 2 digit version.

This was just to do some simple math to calculate how far out from an expiration data a subscription is...."less than 60 days", "less than 30 days", or "less than 15 days". Nothing where the year is critical in determining anything other than subscription expired (and I am using the Ymd that is stored in the database for that calculation).

fengshui

12:57 am on Aug 24, 2008 (gmt 0)

10+ Year Member



Actually, now that you've got me thinking about this again, I just realized that this still isn't going to work properly.

I need to figure out a way to take the date that is stored in the data base in Ymd format (YYYYMMDD) + 10000 to get the expiration date) and compare that (we'll call it $exp) to todays date ($today) and determine if the subscription is within 60, 30, and 15 days respectively of expiration.

Anyways, thanks for the replies so far and will still be hoping someone has a simple answer using the built in functions to accomplish this.

- Dan

cameraman

6:53 am on Aug 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Timestamps are the easiest way to go. If you're on php5 you could fiddle with the DateTime object (look at date_create, date_add, date_sub on the Date/Time functions [php.net] page), but there's not much that date(), strtotime(), and a little math can't do.
$old = strtotime($date);
if($old < strtotime('+15 days'))
echo "You're within 15 days of expiration";

fengshui

7:15 am on Aug 24, 2008 (gmt 0)

10+ Year Member



Outstanding and Thank You. I've joined alot of forums over the years (different ones for javascript, html, paypal etc....) but I have to say this one is now my favorite.

I've gotten (fairly) quick and totally accurate responses without condescending "I'm so smart, why are you so dumb" innuendoes to every question I've posed thus far (the Pay Pal Developers Forum is one of the worst for the "Holier than thou" replies LOL) and this forum covers just about everything (PHP, Javascript, HTML, etc. etc.).

Thank You VERY MUCH again and once again I've learned something new that I can use and take "miles" further down the road. If I'm not mistaken and remember correctly, I think PERL also uses those functions (hopefully the same way)...my first server side scripting experience came from puzzling out an existing PERL script, then buying a book, learning enough to become slightly dangerous (to myself) and writing an online "alumni directory" from scratch (about a year and a half ago).

Been pretty much on PHP and mySql the last 3-4 months (first time working with either) and have passed the "dangerous" point now (I think) and maybe to the point where can finally do some good with it.

- Dan (fengshui)

cameraman

8:25 am on Aug 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



LOL I don't think I've passed 'dangerous' yet..

I've learned some good stuff from the PP forum but I've never had to post anything. I think Webmaster World is the bee's knees, and of course the php forum is the best of the best <grin>.

You're certainly welcome, and good luck with the project. PHP is an awesome language. You'll find that there's generally several different ways to go about doing whatever you want to do.

phranque

8:45 am on Aug 24, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld [webmasterworld.com], dan!

you could probably use mysql to format your date in the query:
DATE_FORMAT function [dev.mysql.com]
mysql date and time types [dev.mysql.com]