When debugging code that stores date/time values in a database, you may find yourself wanting to know the date/time that corresponds to a given unix timestamp, or the timestamp for a given date & time.

The following script will do the conversion either way. If you give it a numeric timestamp, it will display the corresponding date and time. If you give it a date and time (in almost any standard format), it will display the timestamp.

This dateDiff() function can take in just about any timestamp, including UNIX timestamps and anything that is accepted by strtotime(). It returns an array with the ability to split the result a couple different ways. I built this function to suffice any datediff needs I had. Hope it helps others too.

I ran into an issue using a function that loops through an array of dates where the keys to the array are the Unix timestamp for midnight for each date. The loop starts at the first timestamp, then incremented by adding 86400 seconds (ie. 60 x 60 x 24). However, Daylight Saving Time threw off the accuracy of this loop, since certain days have a duration other than 86400 seconds. I worked around it by adding a couple of lines to force the timestamp to midnight at each interval.

I needed a function that determined the last Sunday of the month. Since it's made for the website's "next meeting" announcement, it goes based on the system clock; also, if today is between Sunday and the end of the month, it figures out the last Sunday of *next* month. lastsunday() takes no arguments and returns the date as a string in the form "January 26, 2003". I could probably have streamlined this quite a bit, but at least it's transparent code. =)

<?php
/* The two functions calculate when the next meeting will
be, based on the assumption that the meeting will be on
the last Sunday of the month. */

1) If the year divides by 4, it is a leap year (1988, 1992, 1996 are leap years)2) Unless it divides by 100, in which case it isn't (1900 divides by 4, but was not a leap year)3) Unless it divides by 400, in which case it is actually a leap year afterall (So 2000 was a leap year).

In practical terms, to work out the number of days in X years, multiply X by 365.2425, rounding DOWN to the last whole number, should give you the number of days.

The result will never be more than one whole day inaccurate, as opposed to multiplying by 365, which, over more years, will create a larger and larger deficit.

I do not have much programming in php and I hope I can help those that I want to do is that when entering in the form the date 1 and the date2 I calculate if between those two dates if they have passed 5 or more years and I add 3 more days taking As reference date 2, I do not know if I understand.

To see a theoretical example date = 10/01/2012 date2 = 23/07/2017

Between these two dates have passed 5 years, 6 months, and 13 days elapsed

Knowing this my serious conditional

If they are equal or more than 5 years but less than 10 years will be added 3 daysIf they are equal or more than 10 years but less than 15 years will be added 6 daysIf they are equal or more than 15 years but less than 20 years will be added 9 daysIf they are equal to or more than 20 years will be added 12 days

Then having the conditionals

For this example would be case as it is greater than 5 years the result that I should show taking in days like date of departure the date2 = 23/07/2017 and to this date it is added 30 days that would be a constant and depending on the years like Is the example happens 5 years would be: date2 = 23/07/2017 + 30 days = 08/30/2017 + 3 days= End date to show = result = 02/09/2017.

But I still think how to do it if you can guide me I would appreciate a world, and everything should show without pressing buttons, if I press a button would be like to store in the database only the results otenidos as date1 date2 and result

<? Php

// $ date1 = $ _ POST ["date1"]; // this will be the first date or date of entry// $ date2 = $ _ POST ["date2"]; // this will be the date with which you will calculate// $ difference = $ date2 - $ date1;

I needed to calculate the week number from a given date and vice versa, where the week starts with a Monday and the first week of a year may begin the year before, if the year begins in the middle of the week (Tue-Sun). This is the way weekly magazines calculate their issue numbers.

Here are two functions that do exactly that:

Hope somebody finds this useful.

Gary

<?php
/* w e e k n u m b e r -------------------------------------- //
weeknumber returns a week number from a given date (>1970, <2030)
Wed, 2003-01-01 is in week 1
Mon, 2003-01-06 is in week 2
Wed, 2003-12-31 is in week 53, next years first week
Be careful, there are years with 53 weeks.
// ------------------------------------------------------------ */

I needed a way to display an announcement on a shopping site, that would warn users that orders placed between a certain date range, would not be shipped until after a certain date.

I created this simple date detection code to display the notice on certain pages. You can just copy the code and save it to a file on the site and include it anywhere you need to perform a function, or display a notice.

<?php/* Code to show a message only for a certain time frame. This is a simple include file that can be used to display a message on any pages that use it. Simply us a standard include instruction to this file on the page/s where you want the notice to appear. Written by Hans Kiesouw - hans at wotworx dot com*/

if ($start <= $curdate && $curdate <= $endDate) {/* The message below will appear if the current date is between the start and endDate - used standards HTML to ensure that any code will not be escaped by PHP. You can use any code here to wish to execute for the date range. */?>

<p><strong><font color="#FF0000">Please Note: Any orders placed between July 30th and August 7th will only be shipped on August 8th. We apologize for any inconvenience and thank you for your order.</font></strong></p><?php // don't forget this last bit, it ends the if statement!}

I had some problems with dates between mySQL and PHP. PHP had all these great date functions but I wanted to store a usable value in my database tables. In this case I was using TIMESTAMP(14) <or 'YYYYMMDDHHMMSS'>.
This is perhaps the easiest way I have found to pull the PHP usable UNIX Datestamp from my mySQL datestamp stored in the tables:

This is an easily extendable and pretty way to output human-readable date differences such as "1 day 2 hours ago", "6 months ago", "3 years 7 months 14 days 1 hour 4 minutes 16 seconds" etc etc.
Change "$levels = 2;" to whatever you want. A value of 1 will limit to only one number in the result ("3 days ago"). A value of 3 would result in up to three ("3 days 1 hour 2 minutes ago")

It can be used in the following ways:
echo compare_dates($start_date,$end_date);
echo compare_dates($end_date,$start_date);
echo compare_dates($start_date); //end date will be assumed as time();

With PHP 5.1 and 5.2 the languages datetime support has changed. Although these functions should guess your local timezone settings, they may fail if using a default configuration in a "pre-5.1 way", which means setting no timezone for PHP. In the case PHP could not get a timezone it emits a E_STRICT warning. Note that this affects _all_ datetime functions and keep it in mind when porting software from previous versions to 5.1 or later! It may also confuse your error handling (this is the way I noticed that things have changed, since these changes are not documentated _here_).

I was looking for a solution where I could return the number of days, hours, Minutes and seconds between two entries in a table.DATE_DIFF is not running on my mysql server as my provider uses mysql version 4.0.25Solution was to use to days and std time functions to calculate the difference in one call.The fields stored in the table(report_table) are time(00:00:00),date(0000-00-00) and record(enum) which tells the app the type of log stored. EG start or end of a report.