Subtract one second from a given time - php

I would like to add 1 day and then subtract ( minus ) 1 second from a given time.
I did:
$fromDate = date("Y-m-d", strtotime("2012-09-28")).' 00:00:00';
$date = strtotime(date("y-m-d H:m:s", strtotime($fromDate)) . " +1 day") - 1;
$toDate = date('Y-m-d H:m:s', $date);
echo $toDate;
but instead of 2012-09-28 23:59:59 it returns 2012-09-29 00:09:59
What am I doing wrong?

You're going round and round instead of getting to the point in your code. Here's my solution with DateTime objects:
$time = new DateTime("2012-09-28");
$time->modify("+1 day");
$time->modify("-1 second");
var_dump($time);
Or, if you just need the last second of the day, why not just:
$time = "2012-09-28";
$time .= " 23:59:59";
As it's unlikely that the number of seconds/minutes/hours a day to change.

If I understand you right, you just want the last second in the given day, right?
If that's the case, then you could just have:
$theDate = "2012-09-28";
$fromDate = $theDate." 00:00:00";
$toDate = $theDate." 23:59:59";

Related

How to add 2 hours to variable in php

I am trying to add 2 hours to $server_time and call it $hourPlusTwo. Everything i have tried ends up as either something like 7200 or some obscure date from 1969. How would you do it with what i have here, or rewritten a completely different way? Please understand i am new to php and programming in general. I am trying to understand how to do it, what would be better, and why it works.Thanks in advance.
date_default_timezone_set('America/Los_Angeles');
require_once('mysql_connection.php');
//analyse data by variable time period.
$hour_position = 45;
$htime = -30;
$date = date('Y-m-d H:i:s');
$date = strtotime($date);
$date = strtotime($htime." day", $date);
$date = date('Y-m-d H:i:s',$date);
$qry = "SELECT `last`,`server_time`,`vol` FROM `btce_btc_ticker` WHERE `server_time` > '$date' AND EXTRACT(MINUTE FROM `server_time`) = $hour_position ORDER BY `server_time` ASC";
$price_history_qry = mysqli_query($con,$qry);
while($result = mysqli_fetch_array($price_history_qry)){
$server_time = $result['server_time'];
$server_time = date("m-d-Y (H:i)",strtotime($server_time));
echo 'Server Hour ='.$server_time.'<br>';
echo 'Two Hour ='.$hourPlusTwo.'<br>';
}
You have your time format like this date('Y-m-d (H:i)
Whatever the time you got from the server you just need to speccify your format and add +2 hour from it.
The Change would be
$YourNewDate = date('Y-m-d (H:i)', strtotime('+2 hour'));
And the Result would be 2015-04-05 (23:05) some value like this format.
Update :
As you want to do the increment from the time you have from db
<?php
$result['server_time'] = '2014-04-18 19:56:00';
$server_time = $result['server_time'];
$hourPlusTwo = DateTime::createFromFormat("Y-m-d H:i:s", $server_time);
echo date('Y-m-d (H:i)', strtotime('+2 hours', $hourPlusTwo->getTimestamp()));
?>

How to get the current date in PHP, and add 1 month to the current date?

I'm coding a script where I require to save the current date, and the date 1 month from that date. I am pretty sure that the time() variable works, but I am not sure how to +1 month onto that?
Any ideas, suggestions. Cheers!
Try this
$today = date("Y-m-d");
$date = date('Y-m-d', strtotime('+1 month', $today));
or use DateTime()
$dt1 = new DateTime();
$today = $dt1->format("Y-m-d");
$dt2 = new DateTime("+1 month");
$date = $dt2->format("Y-m-d");
$time = strtotime("2010-12-11");
$final = date("Y-m-d", strtotime("+1 month", $time));
(OR)
strtotime( "+1 month", strtotime( $time ) );
this returns a timestamp that can be used with the date function
Use this:
Current Date:
echo "Today is " . date("Y/m/d");
1 Month to the Current Date:
$time = strtotime(date("Y/m/d"));
$final = date("Y-m-d", strtotime("+1 month", $time));
<?php
$current_time = date("Y-M-d h:i:s",time()); // Getting Current Date & Time
print $current_time; // Current Date & Time Printing for display purpose
$future_timestamp = strtotime("+1 month"); // Getting timestamp of 1 month from now
$final_future = date("Y-M-d h:i:s",+$future_timestamp); // Getting Future Date & Time of 1 month from now
print $final_future; // Printing Future time for display purpose
?>
shorter : $today=date("Y-m-d"); $date=
This one liner worked for me:
$monthFromToday = date("Y-m-d", strtotime("+1 month", strtotime(date("Y/m/d"))));
The given answers may not give you the results you might expect or desire.
Consider:
$today = "29Jan2018";
$nextMonth = date('dMY', strtotime('+1 month', (strtotime($today))));
echo $today // yields 29Jan2018
echo $nextMonth // yields 01Mar2018
$today = date("Y-m-d");
$enddate = date('Y-m-01',strtotime($today. ' + 1 months'));
You could also consider using the Carbon package.
The solution would look like this:
use Carbon\Carbon
$now = Carbon::now;
$now->addMonth();
Here is the link for reference https://carbon.nesbot.com/docs/

Issues with Adding 1 month to a Unix time stamp

For some reason, I cannot get strtotime('+1 month) to work. Here is my code;
$Date = $_REQUEST['date']; //This is a unix time stamp
$Start = $_REQUEST['start']; //This is a unix time stamp
$End = $_REQUEST['end']; //This is a unix time stamp
to add a month onto my dates;
$monStart =strtotime('+1 month', $Start);
$monEnd =strtotime('+1 month', $End);
$monDate =strtotime('+1 month', $Date);
then to show my changed dates;
$vEnd = date('m/d/Y', $monEnd);
$vStart = date('m/d/Y', $monStart);
$vDate = date('m/d/Y', $monDate);
The problem that I have is that the supplied dates;
$Date = 1/31/2013
$Start = 1/01/2013
$End = 1/31/2013
Return;
$vDate = 3/03/2013
$vStart = 2/01/2013 //Only correct one
$vEnd = 3/03/2013
Please can someone help me?
It's jumping to March because today is 31sth Jan, and adding a month gives 31st Feb, which doesn't exist, so it's moving to the next valid date. This is a PHP bug. You can get more info on that at https://bugs.php.net/bug.php?id=44073
You can try with DateTime to over come this scenario. You can use this function for your requirement
function add_month($date_value, $months, $format = 'm/d/Y') {
$date = new DateTime($date_value);
$start_day = $date->format('j');
$date->modify("+{$months} month");
$end_day = $date->format('j');
if ($start_day != $end_day)
$date->modify('last day of last month');
return $date->format($format);
}
Now you can call :
$vEnd = add_month($monEnd, 1);
$vStart = add_month($monStart, 1);
$vDate = add_month($monDate, 1);
This will give you :
$vDate = '02/28/2013';
$vStart = '02/01/2013';
$vEnd = '02/28/2013';
Hope this helps you :)
DateTime is much better for handling date math as it account for things like days in the month:
$dt = new DateTime('2013-02-01');
$dt->modify('+1 month');
echo $dt->format('Y-m-d');
See it in action
Since you're using timestamps it might look like this:
$dt = new DateTime('#'.$_REQUEST['start']);
$dt->modify('+1 month');
echo $dt->format('m/d/Y');

Adding three months to a date in PHP

I have a variable called $effectiveDate containing the date 2012-03-26.
I am trying to add three months to this date and have been unsuccessful at it.
Here is what I have tried:
$effectiveDate = strtotime("+3 months", strtotime($effectiveDate));
and
$effectiveDate = strtotime(date("Y-m-d", strtotime($effectiveDate)) . "+3 months");
What am I doing wrong? Neither piece of code worked.
Change it to this will give you the expected format:
$effectiveDate = date('Y-m-d', strtotime("+3 months", strtotime($effectiveDate)));
This answer is not exactly to this question. But I will add this since this question still searchable for how to add/deduct period from date.
$date = new DateTime('now');
$date->modify('+3 month'); // or you can use '-90 day' for deduct
$date = $date->format('Y-m-d h:i:s');
echo $date;
I assume by "didn't work" you mean that it's giving you a timestamp instead of the formatted date, because you were doing it correctly:
$effectiveDate = strtotime("+3 months", strtotime($effectiveDate)); // returns timestamp
echo date('Y-m-d',$effectiveDate); // formatted version
You need to convert the date into a readable value. You may use strftime() or date().
Try this:
$effectiveDate = strtotime("+3 months", strtotime($effectiveDate));
$effectiveDate = strftime ( '%Y-%m-%d' , $effectiveDate );
echo $effectiveDate;
This should work. I like using strftime better as it can be used for localization you might want to try it.
Tchoupi's answer can be made a tad less verbose by concatenating the argument for strtotime() as follows:
$effectiveDate = date('Y-m-d', strtotime($effectiveDate . "+3 months") );
(This relies on magic implementation details, but you can always go have a look at them if you're rightly mistrustful.)
The following should work,Please Try this:
$effectiveDate = strtotime("+1 months", strtotime(date("y-m-d")));
echo $time = date("y/m/d", $effectiveDate);
Following should work
$d = strtotime("+1 months",strtotime("2015-05-25"));
echo date("Y-m-d",$d); // This will print **2015-06-25**
Add nth Days, months and years
$n = 2;
for ($i = 0; $i <= $n; $i++){
$d = strtotime("$i days");
$x = strtotime("$i month");
$y = strtotime("$i year");
echo "Dates : ".$dates = date('d M Y', "+$d days");
echo "<br>";
echo "Months : ".$months = date('M Y', "+$x months");
echo '<br>';
echo "Years : ".$years = date('Y', "+$y years");
echo '<br>';
}
As of PHP 5.3, DateTime along with DateInterval could be a feasible option to achieve the desired result.
$months = 6;
$currentDate = new DateTime();
$newDate = $currentDate->add(new DateInterval('P'.$months.'M'));
echo $newDate->format('Y-m-d');
If you want to subtract time from a date, instead of add, use sub.
Here are more examples on how to use DateInterval:
$interval = new DateInterval('P1Y2M3DT4H5M6S');
// This creates an interval of 1 year, 2 months, 3 days, 4 hours, 5 minutes, and 6 seconds.
$interval = new DateInterval('P2W');
// This creates an interval of 2 weeks (which is equivalent to 14 days).
$interval = new DateInterval('PT1H30M');
// This creates an interval of 1 hour and 30 minutes (but no days or years, etc.).
The following should work, but you may need to change the format:
echo date('l F jS, Y (m-d-Y)', strtotime('+3 months', strtotime($DateToAdjust)));
public function getCurrentDate(){
return date("Y-m-d H:i:s");
}
public function getNextDateAfterMonth($date1,$monthNumber){
return date('Y-m-d H:i:s', strtotime("+".$monthNumber." months", strtotime($date1)));
}

php - match day of week in date() / time()

I'm trying to reschedule recurring events based on the recurring method: daily, weekly or monthly.
So assuming I have this data:
$now = time();
$start_date = '01/01/2011 14:00';
$end_date = '01/01/2011 14:20';
$start_time = date('H:i', $start_date);
$end_time = date('H:i', $end_date);
$period = $end_date - $start_date; // in this case, 20 minutes
This event rescheduled daily would be:
$new_time_start = strtotime(date("m/d/Y {$start_time}", $now));
$new_time_end = $new_time_start + $period;
And monthly:
$start_day = date('d', $start_date);
$new_time_start = strtotime(date("m/{$start_day}/Y {$start_time}", $now));
$new_time_end = $new_time_start + $period;
It seems to be working, but I don't know how to do the same for weekly events :(
For example '01/01/2011' is Saturday, so I want to reschedule the event to run every Saturday (day 6 of the week according to PHP).
Any ideas?
Give this a try...
$today = date('w');
$day = date('w', $start_date);
$diff = abs(7 - ($today - $day));
$new_time_start = strtotime('+'.$diff.'days', strtotime(date("m/d/Y {$start_time}")));
$new_time_end = $new_time_start + ($end_date - $start_date);
It seems as though you're missing some strtotime( ) wrappers around your $start_date and $end_date values. I'm assuming your storing your start and end date variables as unix timestamps, so I didn't include them in my code either.
Also, just as a side note, you don't have to enter time( ) in date( ) as the second argument. If the time value is missing, it will use the current time. So you can remove all your $now instances and it will still work.
I'm not too sure I understand your question, but can't you do this?
$time = '2011-01-31';
$weekLater = date('Y-m-d', strtotime('+7 days ' . $time));
echo $time . ' -> ' . $weekLater;
Output : 2011-01-31 -> 2011-02-07
Whatever date you give it, it will give you a date 7 days in the future. Is this what you need?

Categories