So first things first is DateDiff() the way to go?
<?php
$date1 = strtotime(1766253900); // expiry date
$date2 = strtotime(1539652560); // todays date
$dtDiff = $date1 - $date2;
$totalDays = intval($dtDiff/(24*60*60));
echo $totalDays
?>
I tried it this way but it is outputting 0 ZERO
This is how you would do this using the DateTime and DateInterval classes:
// set expiry date
$date1 = (new DateTime())->setTimestamp(1766253900);
// today's date
$date2 = new DateTime();
$diff = $date1->diff($date2);
echo $diff->days;
Output (as of 15/10/2018)
2622
Just remove strtotime as both dates are already UNIX timestamps i.e. number of seconds since 1970
<?php
$date1 = 1766253900; // expiry date
$date2 = 1539652560; // todays date
$dtDiff = $date1 - $date2;
$totalDays = intval($dtDiff/(24*60*60));
echo $totalDays
?>
Because you input the 1766253900 and 1539652560 are already timestamps, the strotime function is make the normal date transform the UNIX time, so you needn't use strotime convert the UNIX time to UNIX time
<?php
$date1 = 1766253900; //expiry date
$date2 = 1539652560; //todays date
$totalDays = intval(($date1-$date2)/(24*60*60));
print_r($totalDays);
you can use mysql function convert UNIX time to normal time
mysql> select from_unixtime(1766253900); #2025-12-21 02:05:00
but also you can use mysql function convert normal time to UNIX time
mysql> select unix_timestamp('2025-12-21 02:05:00'); #1766253900
Related
I can add x week to my date
//$ultima_azione <--- 2015/07/15
//$data['intervallo'] <---- 5
$mydate = date("Y-m-d",strtotime($ultima_azione." +".$data['intervallo']." weeks"));
now how can i give a day starting from that week
example:
//$mydate + "next Monday" -----> final date
and this ve to work like, if today is Monday and i add weeks to jump to an other Monday and then i select the next Monday the week don't ve to change
The simplest way would be to use strtotime. It can do date calculations based on a textual representation of the delta:
$mydate = strtotime('+3 weeks');
It also accepts a second parameter, which is a timestamp to start from when doing the calculation, so after you get the offset in weeks, you can pass the new date to a second calculation:
// Get three weeks from 'now' (no explicit time given)
$mydate = strtotime('+3 weeks');
// Get the Monday after that.
$mydate = strtotime('next Monday', $mydate);
See strtotime documentation for more examples of notations that you can use.
I would highly recommend using PHP's built-in DateTime class for any date and time logic. It's a much better API than the older date and time functions and creates much cleaner and easier to read code.
For example:
// Current date and number of weeks to add
$date = '2015/07/15';
$weeks = 3;
// Create and modify the date.
$dateTime = DateTime::createFromFormat('Y/m/d', $date);
$dateTime->add(DateInterval::createFromDateString($weeks . ' weeks'));
$dateTime->modify('next monday');
// Output the new date.
echo $dateTime->format('Y-m-d');
References:
DateTime.
DateTime::createFromFormat
DateTime::add
DateTime::modify
DateInterval::createFromDateString
DateTime::format
Are you looking for something like this?
$today = time();
$weeks = 2;
// timestamp 2 weeks from now
$futureWeeks = strtotime("+ ".$weeks." weeks");
// the next monday after the timestamp date
$futureMonday = strtotime("next monday",$futureWeeks);
echo date("Y-m-d", $futureMonday);
// or in one line
echo date("Y-m-d", strtotime("next monday", strtotime("+ ".$weeks." weeks")));
PHP is using an unix timestamp for date calculations. Functions as date() and strtotime() using a timestamp as an optional second parameter. This is used a reference for formatting and calculations. If no timestamp is passed to the function the current timestamp is used (time()).
I have the answer here. This will show the next wednesday every 2 weeks and the first date to start from would be the 10th.
I have also added in an estimated delivery which would be 6 weeks after that date.
We will be placing our next order for this on:
<?php
$date = '2020/05/26';
$weeks = 2;
$dateTime = DateTime::createFromFormat('Y/m/d', $date);
$dateTime->add(DateInterval::createFromDateString($weeks . ' weeks'));
$dateTime->modify('wednesday');
echo $dateTime->format('d/m/Y');
?>
Expected delivery for the next order will be:
<?php
$date = '2020/05/26';
$weeks = 2;
$dateTime = DateTime::createFromFormat('Y/m/d', $date);
$dateTime->add(DateInterval::createFromDateString($weeks . ' weeks'));
$dateTime->modify('+42 days next wednesday');
echo $dateTime->format('d/m/Y');
?>
If anyone can confirm this is correct that would be great.
Im trying to add a certain amount of days to a timestmp using this in PHP:
$capturedDate = '2008-06-20';
$endDate = strtotime($capturedDate);
$endDate2 = strtotime('+1 day',$endDate);
echo $endDate2;
but its displaying: 1216526400
any ideas?
Try:
echo date("Y-m-d H:i:s",$endDate2);
Or (for just the date):
echo date("Y-m-d",$endDate2);
You can find documentation about how to format your string here: http://php.net/manual/en/function.date.php
You should be using DateTime for working with dates. It's timezone friendly.
$datetime = new DateTime('2008-06-20');
$datetime->modify('+1 day');
echo $datetime->getTimestamp();
strtotime() converts the date into a unix timestamp which is the number of seconds since January 1st 1970. If you want a date output you have to run the finished timestamp through date() first.
$capturedDate = '2008-06-20';
$endDate = strtotime($capturedDate.' +1 day');
echo date("Y-m-d", $endDate);
strtotime creates a Unix timestamp so if you want to be presented with a formatted date, you need to pass the timestamp as an argument to the date function as follows:
$capturedDate = '2008-06-20';
$endDate = strtotime($capturedDate);
$endDate2 = strtotime('+1 day',$endDate);
echo date('Y-m-d', $endDate2);
Additionally, there are a wide variety of parameters you can use in the date function if you want to display additional information.
e.g.: echo date('Y-m-d H:i:s', $endDate2); or echo date('Y-m-d h:i:s a', $endDate2);, etc.
Sooooo close, just take your timestamp and convert it back into date format using date("desired format",$endDate2);
DateTime is a very nice way to deal with dates. You can try like this:
$capturedDate = '2008-06-20';
$date = DateTime::createFromFormat('Y-m-d', $capturedDate)->modify('+1 day');
echo $date->getTimestamp();
I want to compare my system date from the date from MySQL database and the comparison should be like if the system date has the diffrence of 10 days than the date from my table's date.
For e.g. my sytem date: 2013-7-31 and the table's date is 2013-8-10 it should echo a msg "10 days left for renewal."
I know how to add days into a date in php but I have no idea of how should I compare these two.
$mydt = date("Y-m-d");
$alertday = date('Y-m-d', strtotime($mydt. " + 10 days"));
Use date_diff()-> DateTime::diff
$interval = date_diff($date1, $date2); //$date1, $date2 are DateTime objects.
echo $interval->d; //Outputs number of day difference.
More functionality can be found here:
http://www.php.net/manual/de/class.dateinterval.php
http://www.php.net/manual/de/datetime.diff.php
in your query let say you pass in the value from $now = strttotime("now");
you can then do the following in MySQL
WHERE DATE_FORMAT($now "%Y-%m-%d") = DATE_FORMAT(DATE_ADD(DATE_FIELD,INTERVAL -10 DAY), "%Y-%m-%d")
Just convert the db table time to a normal unix timestamp subtract and divide by number seconds in a day
$now = time();
$dbtime = strtotime($tabledate);
$daysleft = ($now-$dbtime) / ( 60*60*24);
Hey i would like to know if there is any script (php) that could check if a specified date three days before today.
say..
$d1 = date("Y-m-d", filemtime($testfile));
$d2 = date("Y-m-d");
now i would like to know how to compare this two dates to check if d1 is atleast 3days ago or before d2
any help would be gladly appreciated.
Why not to use DateTime object.
$d1 = new DateTime(date('Y-m-d',filemtime($testfile));
$d2 = new DateTime(date('Y-m-d'));
$interval = $d1->diff($d2);
$diff = $interval->format('%a');
if($diff>3){
}
else {
}
Assuming you wish to test whether the file was modified more than three days ago:
if (filemtime($testfile) < strtotime('-3 days')) {
// file modification time is more than three days ago
}
Just check it with timestamp:
if (time() - filemtime($testfile) >= 3 * 86400) {
// ...
}
use date("Y-m-d", strtotime("-3 day")); for specific date
you can also use
strtotime(date("Y-m-d", strtotime("-3 day")));
to convert it to integer before comparing a date string
well, stunned to see no one is using mktime() function,
it makes the job simple
for example your input date is :10/10/2012
mktime convert it to unix time stamp
$check_date=mktime(0,0,0,10,**10+3**,2012);
we can perform any operations weather +,-,*,/
use timestamp instead of date,
$d1 = filemtime($testfile);
$now = time();
if ($now - $d1 > 3600*24*3) {
..
}
I'm writing code to subtract two dates. It is for a contract type thingy, where user gets to see the number of days left for his contract to complete. Something like start_date_time="today" and end_date_time=y where the value of y is retrieved from the database (DATETIME type). It is in the mysql datetime format (yyyy-mm-dd HH:mm:ss).
<?php
include_once '../include/connections.php';
$id =$_REQUEST['uid'];
$result= mysql_query("SELECT * FROM data WHERE uid = '$id'");
$test = mysql_fetch_array($result);
echo $test[14];
echo "<br /><br />";
$today=time();
$enddate=strtotime('$test[14]');
$timediff = $enddate - $today;
$days=intval($timediff/86400);
$remaining=$timediff%86400;
$hours=intval($remaining/3600);
$remaining=$remaining%3600;
$mins=intval($remaining/60);
$secs=$remaining%60;
echo "<br>".$days.' days '.$hours.' hours '.$mins.' minutes and '.$secs.' seconds.';
?>
When I echo $test[14];, I get the date and time as stored in the database which is
(2012-09-26 00:00:00)
When i echo $today; then i get it in this format 1348381896. Now, how do i convert this format to the one retrieved from the db so that i can subtract the 2 dates and get the number of days and time left?
You can use these 2 functions to convert dates to each other,
Use this for timestamp to MySQL datetime:
$timestamp = '1348381896';
$date = date('Y-m-d H:i:s', $timestamp);
echo $date;
Use this one for MySQL datetime to timestamp:
$date = '2012-09-26 00:00:00';
$timestamp = strtotime($date);
echo $timestamp;
Also if you are willing to Subtract your dates in MySQL side, you can use the DATEDIFF and or TIMEDIFF functions:
Also you can work with timestamps in MySQL too, using TIMESTAMPDIFF and UNIX_TIMESTAMP functions ...
You could use PHP's DateTime classes for this:-
$today = new DateTime();
$mysqlDate = "2012-12-15 13:40:20"; //for example
// To match your code this would be $mysqlDate = $test[14];
$mysqlFormat = 'Y-m-d H:i:s';
$endDate = DateTime::createFromFormat($mysqlFormat, $mysqlDate);
$diff = $today->diff($endDate);
echo "You have {$diff->d} days, {$diff->h} hours and {$diff->m} minutes left";
This will give the following output:-
You have 22 days, 6 hours and 5 minutes left
(This will change depending on when you run the code).
$diff is an instance of DateInterval.
date formats acceptable to DateTime::createFromFormat() can be found here http://www.php.net/manual/en/function.date.php