I try to convert current date + 1 month to a final timestamp for Active Directory attribut accountExpire.
But the timestamp returned is wrong if someone can help me. Thanks
$now = strtotime(date('Y-m-d H:i:s'));
$final = date('Y-m-d H:i:s', strtotime('+1 month', $now));
$dateTime = new DateTime($final);
$timestamp = $dateTime->format('U');
echo $timestamp;
You only need
$timestamp = strtotime('+1 month');
I had to look it up, but it turns out that LDAP timestamp is not the same as UNIX timestamp. LDAP timestamp is counted in 100s of nanoseconds and is counted from year 1601. You can easily convert one into another by applying the difference and multiplying by 10000000
$UNIXtimestamp = strtotime('+1 month');
$LDAPTimestamp = ($UNIXtimestamp + 11644473600) * 10000000;
Related
I have a date time in 'Y-m-d H:i:s', and i tried to substract the now date with the defined date +1 day to get remaining time in hours, minutes and seconds:
$time = '2017-10-05 14:54:03';
$now = date('Y-m-d H:i:s');
$endTransaction = date('Y-m-d H:i:s', strtotime($time. ' + 1 day'));
$dteDiff = $endTransaction - $now;
echo $dteDiff;
but i always get 0 as the result
You are doing it wrong. The date function returns string so PHP is not able to compare anything. Try with the DateTime class instead. Its diff method returns the DateInterval object with some public properties, like the days property among others, which is the positive integer number (rounded down) of days between two dates:
$now = new \DateTime();
$endTransaction = (new \DateTime('2017-12-05 14:54:03'))->modify('+1 day');
$diff = $endTransaction->diff($now);
printf(
'Difference in days: %d, hours: %d, minutes: %d, seconds: %d',
$diff->days,
$diff->h,
$diff->m,
$diff->s
);
You probably need to use this date_diff
$time = '2017-10-05 14:54:03';
$now = date_create(date('Y-m-d H:i:s'));
$endTransaction = date_create(date('Y-m-d H:i:s', strtotime($time. ' + 1 day')));
$dteDiff = date_diff($now, $endTransaction);
$date = new DateTime($dteDiff);
$result = $date->format('Y-m-d H:i:s');
According to above mentioned description please try executing following code snippet as a solution to it.
$time = '2017-10-05 14:54:03';
$now = strtotime(date('Y-m-d H:i:s'));
$endTransaction = strtotime(date('Y-m-d H:i:s', strtotime($time. ' + 1 day')));
$dteDiff = ($endTransaction - $now)/(24*60*60);
echo round($dteDiff);
$endTransaction and $now are strings.
$time = '2017-10-05 14:54:03';
$now = date('Y-m-d H:i:s');
$endTransaction = date('Y-m-d H:i:s', strtotime($time. ' + 1 day'));
echo($endTransaction."\n");
echo($now."\n");
It prints:
2017-10-06 14:54:03
2017-10-05 11:45:39
The subtraction is not a valid operation for strings. It can handle only numbers. The strings above are converted to numbers. The conversion uses only the leftmost digits present in the string, until it reaches the first character that is not a digit.
Both strings above produce 2017 when they are converted to numbers and their difference is, of course, 0.
The easiest way to work with dates in PHP is to use the DateTime and its related classes.
// Convert the input string to a DateTime object
$then = new DateTime('2017-10-05 14:54:03');
// Add 1 day
$then->add(new DateInterval('P1D'));
// Get the current date and time
$now = new DateTime('now');
// Compute the difference; it is a DateInterval object
$diff = $now->diff($then);
// Display the dates and the difference
echo('Then: '.$then->format("Y-m-d H:i:s\n"));
echo('Now : '.$now->format("Y-m-d H:i:s\n"));
echo('Remaining: '.$diff->format("%R%a days, %h hours, %i minutes, %s seconds.\n"));
The output:
Then: 2017-10-06 14:54:03
Now : 2017-10-05 12:36:25
Remaining: +1 days, 2 hours, 17 minutes, 38 seconds.
I tried to substract two datetimes to get result by seconds like that:
$created = "2015-01-16 07:26:55";
$newdate_created = date('Y-m-d H:i:s', strtotime('+2 month', strtotime($created)));
$now = date('Y-m-d h:i:s');
$interval = date_diff($now, $newdate_created);
$seconds = $interval * 60 * 60 * 12 ;
But i'm getting this error:
date_diff() expects parameter 1 to be DateTime
i dont know where is the problem to getting this working
I'm looking the output to be like that :
5259000 // something like that by seconds
Highly wasteful code. There is NO point in formatting your timestamps into strings, just to have to yank them back into timestamp format:
$newdate_created = strtotime('+2 month', strtotime($created));
$now = time();
$diff_in_seconds = $now - $newdate_created;
I am trying to check how many days were passed since the user last entered the system. I get the last time he\she entered from mysql table column (datetime). so I wrote :
$user_last_visit = $user_info['last_updated']; // 2013-08-08 00:00:00
$user_last_visit_str = strtotime($user_last_visit); // 1375912800
$today = strtotime(date('j-m-y')); // 1250114400
$diff = $today - $user_last_visit_str;
Where $user_info['last_updated'] has the last time he\she visited with the value of 2013-08-08 00:00:00.
After strtotime I get $user_last_visit_str equals to 1375912800
$today has the value of 9-08-13 and after strtotime I get 1250114400.
Some reason $diff = $today - $user_last_visit_str; is negative instead of getting a positive value with 24*60*60*1000 (one day = 24*60*60*1000 ms).
Any ideas?
A simple solution using diff:
echo date_create()->diff(date_create($user_last_visit))->days;
If all else fails, just do:
$diff = floor((time() - $user_last_visit_str) / (60 * 60 * 24));
you can use below code to get date diff, here i given static last date which is 15th july 2013, and taking different from current date.
$last_date = date('y-m-d', strtotime('15th july 2013'));//here give your date as i mentioned below
//$last_date = date('y-m-d', strtotime($your_old_date_val));
$current_date = date('y-m-d');//
echo $date_diff = strtotime($current_date) - strtotime($last_date) ;
echo $val = 60*60*24;
$days_diff = $date_diff / $val;
echo $days_diff;
Try this:
$user_last_visit = $user_info['last_updated']; // considering the value of user last visit is 2013-08-08 00:00:00 which indicates year, month, day, hour, minute, second respectiveliy
$user_last_visit_str = strtotime($user_last_visit);
$today = time();
$diff = $today - $user_last_visit_str;
$no_of_days = ($diff / (60*60*24));
Try using a DateTime object if your version of PHP supports it:
$user_last_visit = DateTime::createFromFormat('Y-m-d H:i:s', $user_info['last_updated']);
$today = new DateTime();
$diff = $today->diff( $user_last_visit, true );
echo $diff->days;
$diff will be a DateInterval object.
try reversing the $today assignment, like so:
$today = strtotime(date('y-m-j'));
that worked for me.
I have a date stored in an array:
$this->lines['uDate']
The format of the date is not fixed. I can be changed with this:
define('DATETIME_FORMAT', 'y-m-d H:i');
How can I increment my uDate with a certain number of days or years?
My question is related to this one:
increment date by one month
However, in my case the date format in dynamic.
So, can I do this?
$time= $this->lines['uDate'];
$time = date(DATETIME_FORMAT, strtotime("+1 day", $time));
$this->lines['uDate']= $time;
date_add()
and consider changes like:
define(DATETIME_FORMAT, 'y-m-d H:i');
$time = date(DATETIME_FORMAT, strtotime("+1 day", $time));
You can use some simple calculation to do it if you have the timestamp.
$date = strtotime($this->lines['uDate']); //assuming it's not a timestamp\
$date = $date + (60 * 60 * 24); //increase date by 1 day
echo date('d-m-y', $date);
$date = $date + (60 * 60 * 24 * 365); //increase date by a year
echo date('d-m-y', $date);
You can also use the mktime() method to do this : http://php.net/manual/en/function.mktime.php
function add_date($givendate,$day=0,$mth=0,$yr=0)
{
$cd = strtotime($givendate);
$newdate = date('Y-m-d h:i:s', mktime(date('h',$cd),
date('i',$cd), date('s',$cd), date('m',$cd)+$mth,
date('d',$cd)+$day, date('Y',$cd)+$yr));
return $newdate;
}
I have found this in PHP help
another useful way, if you want an object rather than a string:
$date = DateTimeImmutable::createFromFormat('Y-m-d', '2022-01-05'); // just an exemplary date
$date = $date->add(date_interval_create_from_date_string('1 day')); // count up
notable difference:
date_add() changes the original object, while DateTimeImmutable::add() does not and simply returns the new object. Depending on the desired behavior, use one or the other.
In certain situations I want to add 1 day to the value of my DATETIME formatted variable:
$start_date = date('Y-m-d H:i:s', strtotime("{$_GET['start_hours']}:{$_GET['start_minutes']} {$_GET['start_ampm']}"));
What is the best way to do this?
There's more then one way to do this with DateTime which was introduced in PHP 5.2. Unlike using strtotime() this will account for daylight savings time and leap year.
$datetime = new DateTime('2013-01-29');
$datetime->modify('+1 day');
echo $datetime->format('Y-m-d H:i:s');
// Available in PHP 5.3
$datetime = new DateTime('2013-01-29');
$datetime->add(new DateInterval('P1D'));
echo $datetime->format('Y-m-d H:i:s');
// Available in PHP 5.4
echo (new DateTime('2013-01-29'))->add(new DateInterval('P1D'))->format('Y-m-d H:i:s');
// Available in PHP 5.5
$start = new DateTimeImmutable('2013-01-29');
$datetime = $start->modify('+1 day');
echo $datetime->format('Y-m-d H:i:s');
If you want to do this in PHP:
// replace time() with the time stamp you want to add one day to
$startDate = time();
date('Y-m-d H:i:s', strtotime('+1 day', $startDate));
If you want to add the date in MySQL:
-- replace CURRENT_DATE with the date you want to add one day to
SELECT DATE_ADD(CURRENT_DATE, INTERVAL 1 DAY);
The DateTime constructor takes a parameter string time. $time can be different things, it has to respect the datetime format.
There are some valid values as examples :
'now' (the default value)
2017-10-19
2017-10-19 11:59:59
2017-10-19 +1day
So, in your case you can use the following.
$dt = new \DateTime('now +1 day'); //Tomorrow
$dt = new \DateTime('2016-01-01 +1 day'); //2016-01-02
Use strtotime to convert the string to a time stamp
Add a day to it (eg: by adding 86400 seconds (24 * 60 * 60))
eg:
$time = strtotime($myInput);
$newTime = $time + 86400;
If it's only adding 1 day, then using strtotime again is probably overkill.
You can use
$now = new DateTime();
$date = $now->modify('+1 day')->format('Y-m-d H:i:s');
You can use as following.
$start_date = date('Y-m-d H:i:s');
$end_date = date("Y-m-d 23:59:59", strtotime('+3 days', strtotime($start_date)));
You can also set days as constant and use like below.
if (!defined('ADD_DAYS')) define('ADD_DAYS','+3 days');
$end_date = date("Y-m-d 23:59:59", strtotime(ADD_DAYS, strtotime($start_date)));
I suggest start using Zend_Date classes from Zend Framework. I know, its a bit offtopic, but I'll like this way :-)
$date = new Zend_Date();
$date->add('24:00:00', Zend_Date::TIMES);
print $date->get();
Using server request time to Add days. Working as expected.
25/08/19 => 27/09/19
$timestamp = $_SERVER['REQUEST_TIME'];
$dateNow = date('d/m/y', $timestamp);
$newDate = date('d/m/y', strtotime('+2 day', $timestamp));
Here '+2 days' to add any number of days.
One liner !
echo (new \DateTime('2016-01-01 +1 day'))->format('Y-m-d H:i:s');