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!

trouble using strftime 1

Status
Not open for further replies.

mrsbean

Technical User
Jul 14, 2004
203
US
I have a date stored in a table as 1964-03-31. I use MySQL's date_format functcion to return the date as 03/31/1964.

Code:
$bdayMonth = strftime("%m", $Birthdate);
echo "<p>$bdayMonth</p>";
$bdayDate = strftime("%d", $Birthdate);
echo "<p>$bdayDate</p>";
$bdayYear = strftime("%Y", $Birthdate);
echo "<p>$bdayYear</p>";

Instead of returning

03

31

1964

it returns

12

31

1969

What am I doing wrong? TIA.

MrsBean
 
the $birthdate must be a unix timestamp, I believe.

i would keep the date as it was and use this kind of thing instead:

Code:
$ts = strtotime($birthdate);
$month = date("m", $ts);
$year = date("Y", $ts);
$day = date("d", $ts);

or even use explode on the mysql date.

you can also use mysql to return the year, month, day etc of a date directly.
[/code]
 
I got pretty much the same result as I did before using the method you provided. The month returned was 12 (instead of 3). The day returned was 31 (same as day provided, as it should be), and the year returned was 1969 instead of 1964.

MrsBean
 
You're not going to be able to use strftime() (or strtotime()) here. Both functions require a Current Unix Epoch timestamp, which enumerates the number of seconds that have passed since 1970-01-01 00:00:00. The number, and unsigned integer, is undefined for dates prior to that date.

But you don't need strftime() or strtotime(). The date value returned from MySQL will be a string, so disassemble the data typographically:

Code:
<?php
list ($year, $month, $day) = split ('-', '1964-03-31');

print $year . '<br>';
print $month . '<br>';
print $day . '<br>';

?>

Returns:

[tt]1964
03
31[/code]

on my system.



Want the best answers? Ask the best questions! TANSTAAFL!
 
Absolutely brilliant. Thank you!

MrsBean
 
@sleipnir214
You're not going to be able to use strftime() (or strtotime()) here. Both functions require a Current Unix Epoch timestamp

i'm surprised. i thought strtotime took a string (such as a mysql timestamp) and converted it to a unix epoch timestamp. you appear to suggest otherwise.
 
jpadie:
I have said nothing of the sort.

[ol][li]UCE timestamps are the count of the number of seconds since 1970-01-01 00:00:00 and are defined to be unsigned integers.[/li][li]mrsbean is reporting trying to manipulate, using strftime() and strtotime(), a date in the year 1964.[/li][li]The year 1964 is before the year 1970, so the UCE timestamp for a date in 1964 would have to be a negative number.[/li][li]Unsigned integers cannot store negative numbers, so a UCE timestamp cannot represent a date/time in 1964.[/li][/ol]

So neither strtotime() nor strftime() will work for dates that occur before 1970. Such is the case here.

mrsbean's code was trying its best to tell us this:[ol][li]The code was returning the year-part, month-part and day-part of the date in 1964 as 1969, 12 and 31, respectively.[/li][li]If UCE timestamps were signed integers, then the UCE timestamp value for 1969-12-31 23:59:59 would be -1.[/li][li]The PHP online manual states that in versions of PHP prior to 5.1.0, strtotime() will return -1 on an error.[/li][/ol]




Want the best answers? Ask the best questions! TANSTAAFL!
 
I read your post to say that strtotime required a valid unix timestamp. I took this from the statement that I quoted above.

I understand your explanation above as it relates to the unix epoch. That was useful, thank you.

The manual reports that this limitation is restricted only to certain platforms running php <5.1.0.
 
It was enough PHP versions and platforms that I got out of the habit of using strtotime() unless I was guaranteed of the right values for date strings. It's good to know that the PHP programmers have worked around the problem.



Want the best answers? Ask the best questions! TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top