Jump to content


Photo

get the difference between two php dates


  • Please log in to reply
6 replies to this topic

#1 n8w

n8w
  • Members
  • PipPipPip
  • Advanced Member
  • 123 posts

Posted 23 July 2006 - 05:27 PM

What is the best way to get the number of days between two dates in this format?
$day1=2006-07-25
$current_date=date("Y-m-d")

I have tried to subtract one from another but always get 0

Also what is the best way to get the weekday of a date?
$theweekday=getweekday(2006-07-25); //Thursday

#2 yonta

yonta
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts

Posted 23 July 2006 - 05:36 PM

You could try this (got it from http://pt.php.net/datetime - see the comments)

//gregoriantojd($month,$day, $year);

$start = gregoriantojd("12", "1", "2006");
$end = gregoriantojd("12", "3", "2006");
$daysdiff = $end - $start;
echo $daysdiff;

do it, do it right, do it right now

#3 Joe Haley

Joe Haley
  • Members
  • PipPipPip
  • Advanced Member
  • 103 posts
  • LocationCanada, eh?

Posted 23 July 2006 - 05:38 PM

Personally, i suggest storing all times and dates as timestamps, then formating them for display. This eliminates the need for more complex comparisons, and allows you to re-format the date for display in any fasion you wish.
Give a man a fish; you have fed him for today.  Teach a man to fish; and you have fed him for a lifetime
Don't teach men to program. Teach them to fish.

Please, try the RTFM solution before asking for help:
http://php.net/manual/en/index.php

#4 Caesar

Caesar
  • Members
  • PipPipPip
  • Advanced Member
  • 1,025 posts

Posted 23 July 2006 - 06:29 PM

This should do what you need:

<?php
  
 $date_a = strtotime("2006-07-10");
 $date_b = strtotime("2006-07-23");
 
 $days = ($date_b - $date_a) / (60*60*24);
 
 echo number_format($days);
 
?>

I also recommend you store dates as timestamps.
PHP Ninja

#5 n8w

n8w
  • Members
  • PipPipPip
  • Advanced Member
  • 123 posts

Posted 23 July 2006 - 10:42 PM

HEY thanks .. works like a charm!!

I agree best to store as datetime .. and format later .. but does timestamp change to the current time everytime the record is modified

#6 Joe Haley

Joe Haley
  • Members
  • PipPipPip
  • Advanced Member
  • 103 posts
  • LocationCanada, eh?

Posted 23 July 2006 - 11:42 PM

but does timestamp change to the current time everytime the record is modified


TIMESTAMP[(M)]

A timestamp. The range is '1970-01-01 00:00:00' to partway through the year 2037.

A TIMESTAMP column is useful for recording the date and time of an INSERT or UPDATE operation. By default, the first TIMESTAMP column in a table is automatically set to the date and time of the most recent operation if you do not assign it a value yourself. You can also set any TIMESTAMP column to the current date and time by assigning it a NULL value. Variations on automatic initialization and update properties are described in Section 11.3.1.2, “TIMESTAMP Properties as of MySQL 4.1”.

In MySQL 4.1, TIMESTAMP is returned as a string with the format 'YYYY-MM-DD HH:MM:SS'. Display widths (used as described in the following paragraphs) are no longer supported; the display width is fixed at 19 characters. To obtain the value as a number, you should add +0 to the timestamp column.

In MySQL 4.0 and earlier, TIMESTAMP values are displayed in YYYYMMDDHHMMSS, YYMMDDHHMMSS, YYYYMMDD, or YYMMDD format, depending on whether M is 14 (or missing), 12, 8, or 6, but allows you to assign values to TIMESTAMP columns using either strings or numbers. The M argument affects only how a TIMESTAMP column is displayed, not storage. Its values always are stored using four bytes each. From MySQL 4.0.12, the --new option can be used to make the server behave as in MySQL 4.1.

Note that TIMESTAMP(M) columns where M is 8 or 14 are reported to be numbers, whereas other TIMESTAMP(M) columns are reported to be strings. This is just to ensure that you can reliably dump and restore the table with these types.

Note: The behavior of TIMESTAMP columns changed considerably in MySQL 4.1. For complete information on the differences with regard to this data type in MySQL 4.1 and later versions (as opposed to MySQL 4.0 and earlier versions), be sure to see Section 11.3.1.1, “TIMESTAMP Properties Prior to MySQL 4.1”, and Section 11.3.1.2, “TIMESTAMP Properties as of MySQL 4.1”.


Via: http://dev.mysql.com...e-overview.html
Give a man a fish; you have fed him for today.  Teach a man to fish; and you have fed him for a lifetime
Don't teach men to program. Teach them to fish.

Please, try the RTFM solution before asking for help:
http://php.net/manual/en/index.php

#7 Caesar

Caesar
  • Members
  • PipPipPip
  • Advanced Member
  • 1,025 posts

Posted 24 July 2006 - 02:29 AM

HEY thanks .. works like a charm!!

I agree best to store as datetime .. and format later .. but does timestamp change to the current time everytime the record is modified


Well, when doing the inserts into your database, you don't have to use the MySQL UNIX_TIMESTAMP (Incase that's what you were thinking)...what I was suggesting, was something more like:

<?php

$stamp = time(); 
//Which would give ya a stamp up to the very second
//Or below...which would give you the same date but not as up-to-the-second
//$stamp = strtotime(date("M d, Y")); 
$sql = "UPDATE table SET lastupdate=$stamp WHERE id='$user[id]' "
mysql_query($sql);

?>

PHP Ninja




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users