Re: [PHP] counting with dates (help!)
On 7 Feb 2002, at 23:21, Sander Peters wrote: Hello, This is my problem: $today = date(Ymd, mktime(0,0,0, date(m),date(d),date(Y))); $last_week = date(Ymd, mktime(0,0,0, date(m),date(d)-7,date(Y))); echo ($today - $last_week); The result is a number like 8876 (20020107-20011231 = 8876) But in date thinking it should be 7! How can I let php count in real days/month/years in stead of numbers? Sanders, I've not prorammed PHP much (Perl), but I looked in the docs and could not find anything. I tested your above code and on my FreeBSD box with PHP Version 4.0.6 it returned 7. Peter Good Perl5/mod_perl/Apache/RDBMS/Unix developer seeking work call 901-652-2002 or email [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] counting with dates (help!)
Sander Peters wrote: Hello, This is my problem: $today = date(Ymd, mktime(0,0,0, date(m),date(d),date(Y))); $last_week = date(Ymd, mktime(0,0,0, date(m),date(d)-7,date(Y))); echo ($today - $last_week); The result is a number like 8876 (20020107-20011231 = 8876) which is completely correct. with date as you use it up there you only generate numerical values in a format you specify, and then you substract those numericals from each other, echoing the rest of 8876 But in date thinking it should be 7! I think if you want to get the number of days you have to 1) substract the mktime values you used in the date statement to get the difference in seconds 2) count how many days you have with this amount of seconds by dividing the seconds through 86400 date is a function to give you representations of a date and time at any given moment as expressed in unix time in a lot of different, configurable formats. after you generated a date representation with date, the program doesn't know anything at all about the date/time meanings of that string or number. hth henning -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
[PHP] counting with dates (help!)
Hello, This is my problem: $today = date(Ymd, mktime(0,0,0, date(m),date(d),date(Y))); $last_week = date(Ymd, mktime(0,0,0, date(m),date(d)-7,date(Y))); echo ($today - $last_week); The result is a number like 8876 (20020107-20011231 = 8876) But in date thinking it should be 7! How can I let php count in real days/month/years in stead of numbers? Maybe this is a silly question, but anyone who has the answer would help me very much! Thanks in advance! -- Met vriendelijke groet / With Greetings, Sander Peters site: http://www.visionnet.nl/ email: mailto:[EMAIL PROTECTED] webmail: mailto:[EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] counting with dates (help!)
You'll need to use Julian format (I think that the name for it) which is DDD - is the year and DDD is the number of days into the year. I'm unsure as to how to do this in PHP so you'll need to do some searching - unless someone knows... Would be nice to know exactly how to do it in case it crops up for me in the future sometime... Martin -Original Message- From: Sander Peters [mailto:[EMAIL PROTECTED]] Sent: Friday, February 08, 2002 9:22 AM To: [EMAIL PROTECTED]; [EMAIL PROTECTED] Subject: [PHP] counting with dates (help!) Hello, This is my problem: $today = date(Ymd, mktime(0,0,0, date(m),date(d),date(Y))); $last_week = date(Ymd, mktime(0,0,0, date(m),date(d)-7,date(Y))); echo ($today - $last_week); The result is a number like 8876 (20020107-20011231 = 8876) But in date thinking it should be 7! How can I let php count in real days/month/years in stead of numbers? Maybe this is a silly question, but anyone who has the answer would help me very much! Thanks in advance! -- Met vriendelijke groet / With Greetings, Sander Peters site: http://www.visionnet.nl/ email: mailto:[EMAIL PROTECTED] webmail: mailto:[EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] counting with dates (help!)
$today = date(Ymd, mktime(0,0,0, date(m),date(d),date(Y))); $last_week = date(Ymd, mktime(0,0,0, date(m),date(d)-7,date(Y))); echo ($today - $last_week); The result is a number like 8876 (20020107-20011231 = 8876) But in date thinking it should be 7! No, that's the difference in time represented by the number of seconds. You still need to work with it a little more. 8876 / 60 = number of hours /* 60 = number of seconds in an hour */ 8876 / 60 / 24 = number of days. /* 24 = number of hours in a day */ Chris
Re: [PHP] counting with dates (help!)
Well, I'll chime in as well. I'd recommend doing all your calculations in timestamps in seconds, then convert the results into days, dates, or whatever. If you only have a date to start with then convert to a timestamp, do the calculation, and convert back. You could wrap it in a function like this: ?php function dateDiffInDays($date1,$date2) { $date1Tm = strtotime($date1); $date2Tm = strtotime($date2); $diff = abs($date1Tm - $date2Tm) / 86400; return $diff; } $today = date(Ymd,mktime(0,0,0, date(m), date(d), date(Y))); $last_week = date(Ymd, mktime(0,0,0, date(m), date(d)-7, date(Y))); print(today: $today br\n); print(last_week: $last_week br\n); print(diff in days: . dateDiffInDays($last_week, $today) . br\n); ? On Thursday, February 7, 2002, at 04:21 PM, Sander Peters wrote: Hello, This is my problem: $today = date(Ymd, mktime(0,0,0, date(m),date(d),date(Y))); $last_week = date(Ymd, mktime(0,0,0, date(m),date(d)-7,date(Y))); echo ($today - $last_week); The result is a number like 8876 (20020107-20011231 = 8876) But in date thinking it should be 7! How can I let php count in real days/month/years in stead of numbers? Maybe this is a silly question, but anyone who has the answer would help me very much! Thanks in advance! -- Met vriendelijke groet / With Greetings, Sander Peters site: http://www.visionnet.nl/ email: mailto:[EMAIL PROTECTED] webmail: mailto:[EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] counting with dates (help!)
RE: [PHP] counting with dates (help!)Hi Sander, (and Chris, and Martin) $today = date(Ymd, mktime(0,0,0, date(m),date(d),date(Y))); $last_week = date(Ymd, mktime(0,0,0, date(m),date(d)-7,date(Y))); echo ($today - $last_week); The result is a number like 8876 (20020107-20011231 = 8876) But in date thinking it should be 7! No, that's the difference in time represented by the number of seconds. You still need to work with it a little more. 8876 / 60 = number of hours /* 60 = number of seconds in an hour */ 8876 / 60 / 24 = number of days. /* 24 = number of hours in a day */ =I'm sorry but neither the above, nor the suggestion of Julian dates was correct (in all cases). The two numbers ($today and $last_week) generated in the PHP code above are in CCYYMMDD format (as used by MySQL to store dates, BTW). =So you are correct (Sander): 20020107 less 20011231 equals 8876 =but this number is meaningless. If the formulae proposed above are applied, the answer is not 7 days. =Similarly (Julian dates = CCYYDDD format) 2002007 less 2001365 equals 642 =However let's jump forward in time, to tomorrow (hey what's 45 minutes between friends?): 20020108 less 20020101 equals 0007 =and: 2002008 less 2002001 equals 007 =woohoo! How come they 'work' tomorrow but not today? Because (using the first format) whilst the last and second to last digits represent days (hence it 'works' tomorrow), the preceding pair of digits represent months, and the procession of days into months is not a decimal progression. (smack your forehead into the wall now...but don't do it too often, because no matter how good it feels, it'll feel a whole lot better when you stop!) =this is also the reason that using a Julian date format won't work - they look like decimal numbers (look like a duck), you can perform arithmetic on them (walk like a duck), but if your calculation spans a year-break you will discover that they are not really decimal numbers (and they bark like a dog). =The three main date formats are: 1 CCYYMMDD (as mentioned above) because it is the way MySQL does things. You can't use this for 'real arithmetic' as we've just discussed, but you can do comparisons, eg is 'today' 'yesterday' (when I was young...) 2 dd-mm-ccyy (or variant) which is the way humans like to read their dates. This is for appearances, and once again not for arithmetic/logic. PS never, never, never (did I say never) use dd/mm/yy or mm/dd/yy because of the ambiguities it causes - particularly between Americans and the rest of the world (and date-guessing functions - see your manual) 3 UNIX Epoch timestamp which is a count of the number of seconds since midnight 1 Jan 1970 (GMT). This is an absolutely ugly way to look at dates (and times), but it is really easy to use for arithmetic and after a while you don't think it at all odd that without any effort you can recall that there are 86,400 seconds in one day. =Now that piece of trivia, a pocket protector, and band-aid keeping your spectacle frames together will make you a babe-magnet in every bar (well those that serve Heineken anyway). =Putting aside several HOURS to study the two manuals to get your head around date functions is time well spent (yes I know, it takes me a little longer...) - particularly the sheer number of SQL date functions (the power of which is all too frequently overlooked). Regarding the PHP functions, I made a page list, and as I made notes about each one, I annotated it with a 'purpose' so that I could keep them straight in my mind. Well, enough of me and there reasons why girls in bars don't talk to me... =Let me do my usual frustrating thing and ask you what you are REALLY wanting to do. You don't actually want to subtract those two numbers/PHP variables, because you already know the answer in their very creation... =Regards, =dn -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] counting with dates (help!)
Soon after I sent my email, I realised that it was wrong - oops :( As Steve Cayford said, use timestamps and then convert it back into whatever format you want I think the best way to work with dates is not to :) find someone else's code that works and use that - dates are awful!! ? $today = mktime(0, 0, 0, date(m), date(d), date(Y)); $last_week = mktime(0, 0, 0, date(m), date(d)-7, date(Y)); $diff = $today - $last_week; echo $diff / 86400; // (60 sec/min * 60 min/hr * 24 hr/day) ? Your comment about not using dd/mm/yy or mm/dd/yy I totally agree with - I use dd-Mon- format for my dates for that reason... Martin -Original Message- From: DL Neil [mailto:[EMAIL PROTECTED]] Sent: Tuesday, January 08, 2002 10:42 AM To: Boget, Chris; 'Sander Peters'; [EMAIL PROTECTED]; Martin Towell Subject: Re: [PHP] counting with dates (help!) RE: [PHP] counting with dates (help!)Hi Sander, (and Chris, and Martin) $today = date(Ymd, mktime(0,0,0, date(m),date(d),date(Y))); $last_week = date(Ymd, mktime(0,0,0, date(m),date(d)-7,date(Y))); echo ($today - $last_week); The result is a number like 8876 (20020107-20011231 = 8876) But in date thinking it should be 7! No, that's the difference in time represented by the number of seconds. You still need to work with it a little more. 8876 / 60 = number of hours /* 60 = number of seconds in an hour */ 8876 / 60 / 24 = number of days. /* 24 = number of hours in a day */ =I'm sorry but neither the above, nor the suggestion of Julian dates was correct (in all cases). The two numbers ($today and $last_week) generated in the PHP code above are in CCYYMMDD format (as used by MySQL to store dates, BTW). =So you are correct (Sander): 20020107 less 20011231 equals 8876 =but this number is meaningless. If the formulae proposed above are applied, the answer is not 7 days. =Similarly (Julian dates = CCYYDDD format) 2002007 less 2001365 equals 642 =However let's jump forward in time, to tomorrow (hey what's 45 minutes between friends?): 20020108 less 20020101 equals 0007 =and: 2002008 less 2002001 equals 007 =woohoo! How come they 'work' tomorrow but not today? Because (using the first format) whilst the last and second to last digits represent days (hence it 'works' tomorrow), the preceding pair of digits represent months, and the procession of days into months is not a decimal progression. (smack your forehead into the wall now...but don't do it too often, because no matter how good it feels, it'll feel a whole lot better when you stop!) =this is also the reason that using a Julian date format won't work - they look like decimal numbers (look like a duck), you can perform arithmetic on them (walk like a duck), but if your calculation spans a year-break you will discover that they are not really decimal numbers (and they bark like a dog). =The three main date formats are: 1 CCYYMMDD (as mentioned above) because it is the way MySQL does things. You can't use this for 'real arithmetic' as we've just discussed, but you can do comparisons, eg is 'today' 'yesterday' (when I was young...) 2 dd-mm-ccyy (or variant) which is the way humans like to read their dates. This is for appearances, and once again not for arithmetic/logic. PS never, never, never (did I say never) use dd/mm/yy or mm/dd/yy because of the ambiguities it causes - particularly between Americans and the rest of the world (and date-guessing functions - see your manual) 3 UNIX Epoch timestamp which is a count of the number of seconds since midnight 1 Jan 1970 (GMT). This is an absolutely ugly way to look at dates (and times), but it is really easy to use for arithmetic and after a while you don't think it at all odd that without any effort you can recall that there are 86,400 seconds in one day. =Now that piece of trivia, a pocket protector, and band-aid keeping your spectacle frames together will make you a babe-magnet in every bar (well those that serve Heineken anyway). =Putting aside several HOURS to study the two manuals to get your head around date functions is time well spent (yes I know, it takes me a little longer...) - particularly the sheer number of SQL date functions (the power of which is all too frequently overlooked). Regarding the PHP functions, I made a page list, and as I made notes about each one, I annotated it with a 'purpose' so that I could keep them straight in my mind. Well, enough of me and there reasons why girls in bars don't talk to me... =Let me do my usual frustrating thing and ask you what you are REALLY wanting to do. You don't actually want to subtract those two numbers/PHP variables, because you already know the answer in their very creation... =Regards, =dn