Jump to content

get the difference between two php dates


n8w

Recommended Posts

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
Link to comment
Share on other sites

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

[code]
//gregoriantojd($month,$day, $year);

$start = gregoriantojd("12", "1", "2006");
$end = gregoriantojd("12", "3", "2006");
$daysdiff = $end - $start;
echo $daysdiff;
[/code]
Link to comment
Share on other sites

This should do what you need:

[code]<?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);

?>[/code]

I also recommend you store dates as timestamps.
Link to comment
Share on other sites

[quote author=n8w link=topic=101570.msg402211#msg402211 date=1153694537]
but does timestamp change to the current time everytime the record is modified
[/quote]

[quote]
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”.
[/quote]

Via: http://dev.mysql.com/doc/refman/4.1/en/date-and-time-type-overview.html
Link to comment
Share on other sites

[quote author=n8w link=topic=101570.msg402211#msg402211 date=1153694537]
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
[/quote]

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

[code]<?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);

?>[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.