
|
View Full Version : Date - 1969 ? How to get rid of it.
horizon 08-17-2006, 01:42 PM Hi,
I'm trying to get rid of the 1969 year when building a date function. Unfortunitely, when calling this general function, it returns 1969 as a value rather than the current date.
Could someone post a valid date function - that represents the following example:
function set_date($argument1, $argument2) {
and so on . . .
Any help on this would be really appreciated. :)
Thanks.
(Note: The number of arguments must be presented as is - 2 - these are not considered an example).
maiahost 08-17-2006, 04:04 PM php has a great time/date built in why would you need such a function ?
brendandonhu 08-17-2006, 04:11 PM Your timestamp is invalid or is before the unix epoch. Can't tell a whole lot from just the function name and argument list though.
maxymizer 08-17-2006, 04:13 PM What does this function do? As maiahost said, php allready has date() function..
horizon 08-17-2006, 04:16 PM Can't tell a whole lot from just the function name and argument list though
Very well. Here it is:
function format_date($format, $timestamp) {
$timezone_offset = "-5";
return date($format, $timestamp + (3600 * $timezone_offset));
}
Any idea on how to customize that ?
horizon 08-17-2006, 04:17 PM As maiahost said, php allready has date() function..
I need this function, since it is globalized - all over the script but can't seem to show the right date format.
maiahost 08-17-2006, 04:23 PM right ... well I saw a thing on timezone customization : http://www.phwinfo.com/forum/showthread.php?p=390588
never had to do it myself but it can be of help I hope
MySQL also has a similar function ... I think ... just google it
horizon 08-17-2006, 04:26 PM Any technical response ? ...
brendandonhu 08-17-2006, 04:28 PM Function works fine for me...what parameters are you passing it?
horizon 08-17-2006, 04:51 PM Function works fine for me
How unfortunite. It never did with me and I tried it on different servers with same results. :(
what parameters are you passing it?
I don't think I understand the question. Could you explain that in details ?
maiahost 08-17-2006, 04:56 PM how about this
<?
echo "Original Time: ". date("h:i:s")."\n";
putenv("TZ=US/Eastern");
echo "New Time: ". date("h:i:s")."\n";
?>
and the list of timezones :
http://www.theprojects.org/dev/zone.txt
horizon 08-17-2006, 05:22 PM Good to know about the timezone but, please, let's focus on the function I posted above. ;)
maiahost 08-17-2006, 05:27 PM sure can you please tell us what the function is intended to do ?
3600 * $timezone_offset with a - sign will always give you a negative date?
horizon 08-17-2006, 05:38 PM It means; the timezone multiplied by an hour (in secs).
maiahost 08-17-2006, 05:49 PM $timezone_offset = "-5" times 3600 (-18000) + $timestamp
nope - seems like I am so sleepy that I can't even tell the difference, please ignore the upper comment and I do wish I could have been of some help :)
Tree NC 08-17-2006, 06:02 PM The likely problem is that your timestamp is incorrectly formatted. Try it without the timestamp. If just date($format,$timestamp) won't work, you have an incorrectly formatted timestamp, format, or there's a deeper problem.
horizon 08-17-2006, 06:15 PM or there's a deeper problem.
That's the one. So, any technical ways - like modifying the code - to resolve this problem ? :)
brendandonhu 08-17-2006, 07:23 PM The function itself is fine, its most likely the parameters you're passing it. Can we see what the value of $format and $timestamp is?
horizon 08-17-2006, 07:45 PM The $format is: "Y-m-d" and the $timestamp is the variable that you're assigning from the SQL table.
Ex:
format_date("Y-m-d", $get_date['date_field']);
brendandonhu 08-17-2006, 07:55 PM Yes, what is the actual value of $timestamp? ($get_date['date_field'] or wherever its coming from)
If this is coming from a database, maybe you've got UNIX and MySQL timestamps mixed up or something like that.
horizon 08-17-2006, 07:59 PM you've got UNIX and MySQL timestamps mixed up or something like that.
I understand. Anyway to resolve this problem from my unix system ? :)
brendandonhu 08-17-2006, 08:03 PM Well first you need to find out what format your timestamps are in. echo $timestamp; inside the function and see what it prints out when you call it.
maxymizer 08-17-2006, 08:26 PM Your $get_data['timestamp'] or whatever you're pulling from your db is 0. Check your database fields for timestamp values.
ochiba 08-17-2006, 09:47 PM The $format is: "Y-m-d" and the $timestamp is the variable that you're assigning from the SQL table.
Ex:
format_date("Y-m-d", $get_date['date_field']);
Hmm... try adding strtotime() to the timestamp.
horizon 08-17-2006, 10:27 PM Your $get_data['timestamp'] or whatever you're pulling from your db is 0. Check your database fields for timestamp values.
False. The readings in my database are perfectly fine. However, I think you're right about something here. Even if the readings are correct, for the timestamp, may be it shows as zero when applying this function (which is what might be the point of this topic after all).
Hmm... try adding strtotime() to the timestamp.
I tried it. Unfortunitely, it still displays as 1969. :(
Any more inputs ?
brendandonhu 08-17-2006, 11:00 PM Can only say it so many times...your value for $timestamp is not a correct timestamp. It is either negative, 0, null, or not in the correct format. You can echo $format in your function to find out what value its getting.
horizon 08-17-2006, 11:15 PM I just tried the echo from function for the $timestamp but it doesn't show anything on the monitor. Still left blank ... odd but perhaps I'm missing something here.
brendandonhu 08-17-2006, 11:28 PM Ok, that means $get_date['date_field'] is empty most likely.
To make sure the function works for you, you can do something like this:
format_date("Y-m-d", time());
horizon 08-17-2006, 11:38 PM Ok, I have applied your suggestion above and, now, it displays correctly. :)
Any idea why it wouldn't display correctly from an SQL field (even thought the SQL field is not empty) ?
wdgbom 08-17-2006, 11:58 PM This will be interesting to watch. I work for a very large webhosting company [which i will not disclose since I don't want to violate any contractual agreements I may have made with them], Anyhow, our provisioning systems sometimes displays this date for pending transactions in this format. My staff received an email on it explaining why it comes up but since it's been a few months, i don't recall the exact details and I'm not going to start guessing either.
It's still the same though, and considering the fact that I work for a very very large hosting company it will be interesting to see what comes up through the board on this. Apparently our internal admins can't figure this out. Then again, maybe there is good reason to leave it this way. Who knows.
Anyhow, when I get back to work on monday I'll check my emails for info on this and provide some input if this hasn't been figured out by then.
==============================
addition..
oops i missed the last post sorry. guess i'll forward that to them to see if they can get it to work the way it should now.
brendandonhu 08-18-2006, 12:28 AM Ok, I have applied your suggestion above and, now, it displays correctly. :)
Any idea why it wouldn't display correctly from an SQL field (even thought the SQL field is not empty) ?
Your variable is not getting the value of the database field. The value you're giving the function is either negative, 0, or empty. Can you post the code that is supposed to put the SQL field into $get_date['date_field'] and maybe we can tell why its not working.
maxymizer 08-18-2006, 05:05 AM There are no two-ways about this problem, brendandonhu and others pointed out one thing - your custom function is getting either 0, null, negative value or a string value (regular text) and that's why date() function fallbacks to 0 giving you 1969. You tried to echo it - you got a blank result.
Solution: either SQL field is empty or it's 0 (you said it's not) or there's a misspell of the variable name in the code where you call the function.
What do you get when you echo $get_date['date_field']?
horizon 08-18-2006, 09:05 AM What do you get when you echo $get_date['date_field']?
Outside the function, I see the field perfectly. It returns the right date. Like I said, it's not an SQL field issue - it's the function period. I need to know what peace is missing in that puzzle.
maxymizer 08-18-2006, 09:25 AM Outside the function, I see the field perfectly. It returns the right date. Like I said, it's not an SQL field issue - it's the function period. I need to know what peace is missing in that puzzle.
You simply won't read what people are telling you..ok, easy way: timestamp is a number of seconds since 1970. It looks like: 1155907287.
Now, date() function takes 2 parameters - first is the date format, the other is timestamp, not a DATE. So you must pass a number to that function instead of a humanly readable date.
So, if you have a date stored in your SQL field (you should have posted what you see when you echo $get_date['date_field'], it would ease the analysis), you need to somehow turn it into a number.
That's where strtotime() (http://www.php.net/strtotime) comes to help (someone allready suggested it).
To sum it up, all you have to do is call your custom set_date() function like this:
set_date($format, strtotime($get_date['date_field']))
If it still doesn't work, please post the value of $get_date['date_field'] (when you echo it before calling the function).
This is actually a trivial problem, it's lacking of info what's making the solution unreachable.
horizon 08-18-2006, 10:05 AM set_date($format, strtotime($get_date['date_field']))
Seem to work. :)
Althought, by adding the strtotime module, wouldn't that ignore the $timezone_offset from my posted function ?
brendandonhu 08-18-2006, 10:26 AM Your function should still work. Just make sure its giving you the right output because strtotime() doesn't always give you the right timestamp for the string you give it.
horizon 08-18-2006, 10:42 AM strtotime() doesn't always give you the right timestamp for the string you give it
If it doesn't always return the right timestamp, is there any other choices than strtotime - to put it static ?
brendandonhu 08-20-2006, 07:12 PM strtotime() needs to understand the format of the string time you give it, if its giving you the right output then you don't need to worry about it.
horizon 08-20-2006, 07:25 PM Then, it would seem I have replied a little bit too fast on this one since it doesn't look it worked after all. :(
maxymizer 08-20-2006, 07:28 PM Then, it would seem I have replied a little bit too fast on this one since it doesn't look it worked after all. :(
It seems that your problem is unsolvable then. After 2 pages of giving instructions and function names that operate with dates and timestamps, you're still in a dead end.
I'm very sorry you encountered such a difficult task.
brendandonhu 08-20-2006, 07:30 PM Then, it would seem I have replied a little bit too fast on this one since it doesn't look it worked after all. :(
We still don't know the value of $timestamp that you're giving the function...
horizon 08-20-2006, 07:34 PM We still don't know the value of $timestamp that you're giving the function...
Yes, it has been mentionned on the 2nd page . . .
brendandonhu 08-20-2006, 07:48 PM We still haven't seen the value of $timestamp. I think 4 or 5 people have asked to see it, we really can't help you if you don't want to post your code.
horizon 08-20-2006, 08:24 PM The value is the current date ... it's like:
2006-08-20
That's the timestamp ... the current date.
brendandonhu 08-20-2006, 08:32 PM Ok...that's not a timestamp. A timestamp is an integer, the number of seconds since January 1, 1970.
You can turn it into a timestamp like this:
$timestamp = explode('/', $timestamp);
$timestamp = mktime(0,0,0,$timestamp[1],$timestamp[2],$timestamp[0]);
Not tested but that should do it.
horizon 08-20-2006, 08:39 PM Sounds good but, from the function I posted, could you make the modifications into that block instead ? :)
brendandonhu 08-20-2006, 08:44 PM function format_date($format, $timestamp) {
$timestamp = explode('/', $timestamp);
$timestamp = mktime(0,0,0,$timestamp[1],$timestamp[2],$timestamp[0]);
$timezone_offset = "-5";
return date($format, $timestamp + (3600 * $timezone_offset));
}
horizon 08-20-2006, 09:50 PM Ok, now it states as:
29.11.2005
Time Offset of: "-5" (supposed to show the actual date). Any other idea ? :)
brendandonhu 08-20-2006, 09:58 PM Whoops, try it this way
function format_date($format, $timestamp) {
$timestamp = explode('-', $timestamp);
$timestamp = mktime(0,0,0,$timestamp[1],$timestamp[2],$timestamp[0]);
$timezone_offset = "-5";
return date($format, $timestamp + (3600 * $timezone_offset));
}
horizon 08-20-2006, 10:35 PM Wow ! almost there - a day behind (showing yesterday - I checked my server time and it is showing 20. As for your last posted function, it shows 19. :blink:).
So, I was right. For the last three pages, I have been trying to explain it's all about a faulty function. We're getting close. :)
Any other solution to boost this to the precise date and server time ?
brendandonhu 08-20-2006, 10:42 PM Your "timestamp" only has the date, month, and year. There's no hours, minutes, or seconds so your script can't decide any more accurately than that whether its the 19th or 20th. You'll need to decide what values to use for the hour and minute if you want it to be more precise than that.
horizon 08-20-2006, 11:17 PM Marvelous. You're absolutely right. I knew someone would be able to discover the truth about this function. Very well, thanks very much for your assistance on this. :)
brendandonhu 08-20-2006, 11:36 PM No problem
Burhan 08-21-2006, 01:46 AM I knew someone would be able to discover the truth about this function.
The truth about this function was discovered by many people on page 2 of this uneccessarily long thread, and was clearly summarized by maximyzer.
1. You don't know the difference between a timestamp and a date field. I suggest you read up on this.
2. Do not assume anything about a database or a function. Just because you "see" something is not empty in a table, doesn't mean that it is going to be that way in your code. You might see 2006-08-20, and it looks like a date, but it may be stored as a string.
3. When people ask for something, instead of ignoring them, oblige them with what they ask. There is a reason why they are asking, because they believe (rightly so) that it is the source of the problem, instead of assuming you know the cause.
To be honest, I'm surprised people actually stuck out this long. With your attitude, I wouldn't even have posted past the first page. You should really be grateful.
horizon 08-21-2006, 08:41 AM I really don't see the point of this argument, since this matter has been resolved. If it has taken three pages to resolved it, it's because the solution wasn't discovered until the end of this page.
1. I may not know the difference the timestamp and the date field, since I didn't built this date function all by myself.
2. Do not assume anything about a database or a function. Just because you "see" something is not empty in a table, doesn't mean that it is going to be that way in your code. You might see 2006-08-20, and it looks like a date, but it may be stored as a string.
Don't take it wrong there, but I didn't understood a word of what you said.
3. When people ask for something, instead of ignoring them, oblige them with what they ask. There is a reason why they are asking, because they believe (rightly so) that it is the source of the problem, instead of assuming you know the cause.
Wrong. I knew what the problem was all along and it was about a faultly coded function and it has, now, been resolved.
To be honest, I'm surprised people actually stuck out this long.
Then, it would seem that I was on the right track for this post since you seem to have missed the point on why it did took three pages.
With your attitude, I wouldn't even have posted past the first page.
If it's your opinion, simply don't post since I was simply looking for a very specific solution in order to resolve this issue.
You should really be grateful.
If I read this entire topic, again, I do not see anywhere where I was ungrateful.
Please keep your critisism for yourself next time since, from this page, I thanked brendandonhu for his help on this matter. If this isn't satisfactory enough for you, then - please - don't post.
Thanks for your understanding.
maxymizer 08-21-2006, 09:44 AM horizon, fyrestrtr was right. It took 3 pages for you to finally tell us what's stored in your table (2006-08-20).
Ok, it's one thing if you are not familliar with terms like timestamp, string, integer, null etc., it's perfectly understandable. But it's completely another thing when you ignore people who are trying to help you and who are asking you for some feedback from your side.
Yes, the matter is resolved because brendanhou has nerves of steel, I personally didn't want to help you any further. Every person in this topic knew what the problem was, it was just your attitude and stubbornes that delayed the solution and that's all. And this problem was so trivial, it's just amazing that it took 3 pages..
You took the criticism from fyrestrtr in the wrong way, which is a bit sad since his intention wasn't to flame you but to instruct you on how to help people to help you and how to act as a community member.
And in the end, there was nothing wrong with the function. Your function worked great from the start. You were passing wrong things to the function. It's like trying to make a pizza with a brick and blaming the stowe for not making the meal properly.
horizon 08-21-2006, 09:49 AM Without wanting to argu further on this, it took three pages since the solution wasn't found until now. It works great and I'm grateful for it. Point made.
maxymizer 08-21-2006, 10:04 AM I just can't help it - solution to your problem wasn't "found", people were trying to EXPLAIN to you that you're not giving enough input. I am sorry for not knowing japanese or other languages as it seems that you do not understand what I am trying to say. Anyway, it's great that your "problem" is solved, but be more considerate in the future. Thank YOU for understanding. Good luck with further development.
horizon 08-21-2006, 12:53 PM Dude, don't take it wrong there - but if you take a look at your latest posted solution - which was this one:
http://www.webhostingtalk.com/showpost.php?p=4060132&postcount=40
your technical advise was that there were no more solution when there was one. Since brendandonhu shorted it up under two single posts, it would seem that it was your last inputs that didn't helped out, as it made prolongation to this topic.
Meaning, if you did not know how to, first, post a technical solution, then - please - don't post it at all and let other experienced users to post their modifications the way it should be correctly handled (especially when it comes from a faulty function issue).
To conclude, if this topic contained more than three pages, you should notice that other users added their inputs, regardless this subject, which made a prolongation of these posts.
With that all said, the final response was posted on two solutions, which was posted by: brendandonhu, and it works - period.
Again, thanks for your understanding on this matter since posters should not be critisized over technical methods that are officially working (or close).
maxymizer 08-21-2006, 01:16 PM I take it that you also require a lesson in something called sarcasm (http://en.wikipedia.org/wiki/Sarcasm);)
I've dealt with enough children in my life and I see no point in further communicating with you, "dude".
Thanks for your understanding, you are free to read the article found at the address posted above.
Best of luck in further development.
Elliot A 08-22-2006, 07:45 AM if you are storing the timezone offset as a string, then try:
$offset = '+5';
echo date ( 'Ymd H:i:s', strtotime ( $offset . ' hours' ) );
horizon 08-22-2006, 08:43 AM Sorry. Can't be done this way since I'm using SQL date and time fields for timestamp.
osteoarthitis 08-30-2006, 04:39 PM when PHP has its own calender why should'not you use them ?
the playing with a lot of own functions indulge us in jargon .
horizon 08-30-2006, 05:01 PM when PHP has its own calender why should'not you use them ?
Because of timezones. ;)
|