Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

convert 20060822121212 to unix timestamp 2

Status
Not open for further replies.
use the function strtotime(). I don't have the time, but you could look through the PHP source code to find the formula to strtotime() if you want.


--== Anything can go wrong. It's just a matter of how far wrong it will go till people think its right. ==--
 
formula = number of seconds since 1 jan 1970.
the function, as zeland correctly points out, that best serves your need is strtotime.
 
@dkdude
that's lazy behaviour. it would have taken you less than a minute to test your predicate

Code:
<?
$input = "20060808121212";
$input_no_seconds = "200608081212";
$timestamp = strtotime($input);
$timestamp_no_seconds = strtotime($input_no_seconds);
echo "$timestamp<br/>$timestamp_no_seconds";
?>

renders
Code:
1155035532
1155035520

from this it can be seen that strtotime handles seconds just fine (which, since it gives a unix timestamp [based on seconds], would be surreal if it didn't)
 
Thanks jpadie.

jpadie said:
that's lazy behaviour. it would have taken you less than a minute to test your predicate

I did actually test. Alway do before posting any code.

Your example tested on PHP 4.2.2, Apache 2.0, Win XP renders:

-1
-1

Can't say why, but that's what I get... any idea?

Regards
 
... in PHP 5.1.5 on Linux it does actually work.

So it's gotta be either the PHP version or the OS that makes the difference.

How odd.
 
prior to php5 microseconds were not allowd and would have produced a result of -1. from v5 onwards they are allowed but ignored.

the -1 is a failure code for php <5.1.0 which indicates that there is something wrong with the input string itself (like an invalid date - note the validity is platform dependent).

 
Thanks,

You say that the validity is platform dependent - so how come I get an error on PHP 4.2.2 + XP?

August 8, 2006 @ 12:12:12 sounds valid to me :)
 
validity is platform dependent in that windows won't let you get a unix timestamp before jan 1 1970 whereas some *nix distros will (basically negative timestamps).

php reports that this limitation is fixed in php 5.1.0

additionally there is a general limitation in dates outside of the acceptable range (broadly 1901->2038).

i'm not sure that the ampersat is part of a valid date-time string literal. the difference may be that php4 did not ignore it but threw an error whereas php5 just ignored the inclusion. In fact the GNU spec for getTime (on which strtotime is based) states that the ampersat is used to tell the parser that the following digits represent a number of seconds from the Epoch. In any event, was not the OP asking about a numeric time stamp conversion (yyyymmddhhmmss)?
 
Yeah, I know about the range issues.

Was just wondering why a legel time (hh:mm:ss) on a legal date in a legal month on a legal year would throw an error.

But let's leave that for now - I'm sure Shrum found his answer somewhere in this thread ;-) which is the most important!
 
Was just wondering why a legel time (hh:mm:ss) on a legal date in a legal month on a legal year would throw an error.

because you included an "@" sign (or you said you did in your post)
 
dkdude said:
Was just wondering why a legel time (hh:mm:ss) on a legal date in a legal month on a legal year would throw an error.
Because the string is not in a legal format. If you use the string "2006-08-08 12:12:12", as God and nature intended, then it works perfectly.

According to the PHP docs, strtotime() uses the GNU date input format, which allows dates to be pure numbers. However, the docs only mention yyyymmdd and hhmm format. There's nothing at all about yymmddhhmmss format. The fact it works on PHP5/Linux is probably just an extra feature of whatever library PHP passes the conversion off to. In other words, a happy accident.
 
Thanks a bunch, AdaHacker [thumbsup2]

Exactly the answer I was looking for. If I replace jpadies $input string with your's it works perfect - even on PHP 4.2.2+XP.

Have a star
 
Wow...I woke up this morning and found like 10 notifications on this one thread. Thought the site was sending me repeat notifications. :)

zelands strtotime works. It's a bit fickle about how the string needs to be formatted but that's fine.

I tried my raw string of 'YYYY.MM.DD @ HH.MM.SS' and it didn't like that. So I simply did a str_replace on the "." and " @ " to "" and sent that to strtotime and that worked.

Thanks for the help.

=============================
Sean Shrum - Shrum Consulting
Blog: Code: Consulting: Updates:
 
Found something out.

On PHP 5.1.5, this works in strtotime():

20060823121212

However on PHP 5.0.5, this generates a '-1' result.

If I format the string like this:

2006-08-23 12:12:12

This works in both versions. Granted, this string conforms to the date/time formatting outlined in the whitepaper.

FYI

=============================
Sean Shrum - Shrum Consulting
Blog: Code: Consulting: Updates:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top