stringtotime from Unix timestamp - php

I am doing the following in my php script and I am not seeing the results I though I would:
$current_date_num = strtotime("now");
echo $current_date_num ." BEFORE DATE HOUR<br/>";
$new_current = date('Y-m-d h:i:s', strtotime($current_date_num));
echo $new_current . " AFTER DATE<br/>";
$current_date_num = date('Y-m-d h:i:s', strtotime($current_date_num) - 60 * 60 * 6);
echo $current_date_num ." CURRENT<br/>";
$end_date = strtotime("+1 day");
$end_date = date("Y-m-d h:i:s",$end_date);
echo $end_date ." END DATE";
This is my output and Only the END DATE is showing what I would expect.
1322673564 BEFORE DATE HOUR
1969-12-31 07:00:00 AFTER DATE
1969-12-31 01:00:00 CURRENT
2011-12-01 12:19:24 END DATE
What I thought would happen is I get the Unix timestamp and then use strtotime and get the current time and then the current time minus 6 hours and finally the time 24 hours from now. Not sure how I am messing this up so bad?

You are applying strtotime() two times too many. One time in line 4:
strtotime($current_date_num)
remove that, and the strtotime() call in line 7.

strtotime takes a human-readable time and parses it into a timestamp. As you already have a timestamp, the strptime calls are generating bogus data.
$current_date_num = strtotime("now");
echo $current_date_num ." BEFORE DATE HOUR<br/>";
$new_current = date('Y-m-d h:i:s', $current_date_num);
echo $new_current . " AFTER DATE<br/>";
$current_date_num = date('Y-m-d h:i:s', $current_date_num - 60 * 60 * 6);
echo $current_date_num ." CURRENT<br/>";
$end_date = strtotime("+1 day");
$end_date = date("Y-m-d h:i:s",$end_date);
echo $end_date ." END DATE";
1322674409 BEFORE DATE HOUR
2011-11-30 06:33:29 AFTER DATE
2011-11-30 12:33:29 CURRENT
2011-12-01 06:33:29 END DATE

Related

How to get the time difference between the start time and endtime having time transition from PM to AM?

I have the below code to calculate the time difference but when i run the code it prints 0 difference. Please see into my code below.
I want to get the time difference between the time interval which has Time Transition from PM (Today) to AM (Next day).
//echo 'Hello World!';
//echo mktime('11','30','0','1','2019','1');
//echo mktime('12','30','0','1','2019','1');
//echo (" hello ");
//echo date(time());
//echo (" hello ");
//echo date('Y-m-d H:i:s A');
$date = "2019-10-16";
$stime = "11:30:00 PM";
$etime = "12:30:00 AM";
if("PM" == date('A', strtotime($stime)) && "AM" == date('A', strtotime($etime)))
{
//echo " Time transition ";
//get the start date and start time merging format
$sdatetime = date('Y-m-d H:i:s A', strtotime("$date $stime"));
//convert the start datetime into epoch
$startepoch = strtotime($sdatetime);
//increment the date to get the next day date
$edate = date('Y-m-d', strtotime($date. ' + 1 day'));
//get the end date and end time merged
$edatetime = date('Y-m-d H:i:s A', strtotime("$edate $etime"));
//convert the end datetime into epoch
$endepoch = strtotime($edatetime);
$timediff = ($endepoch-$startepoch)/3600.0;
echo " Time Difference: ".$timediff;
}else{
echo "No time transition";
}
// 24 hour format
//$time = "00:00:00";
//echo date('A', strtotime($time));
PHP datetime and strtotime can parse times with AM and PM.
DateTime objects can also be compared directly.
If the end time is less than the start time, it is assumed to be the end time of the next day. This is better than selecting between AM and PM.
$date = "2019-10-16";
$stime = "11:30:00 PM";
$etime = "12:30:00 AM";
$dtStime = date_create($date." ".$stime);
$dtEtime = date_create($date." ".$etime);
if($dtEtime < $dtStime){
$dtEtime->modify('+1 Day');
}
$diff = $dtStime->diff($dtEtime);
$seconds = $diff->h *3600 + $diff->i * 60 + $diff->s;
echo "Diff: ".$seconds." Seconds";
returns
Diff: 3600 Seconds

show an alert three days later from the beginning date

I need to make a function which will add to the beginning date 3 days and if it's 3 then it shows alert.
$beginDate = 2016-07-29 17:14:43 (this one is from sql table format)
$dateNow = date now
if $beginDate + 3 days (72h) is >= $dateNow then echo "It's been three days from..."
It seems to be simple but I have hard time to make it work with strtotime() and date() functions. How to code this in PHP?
You can do something like this
$beginDate = "2016-07-29 17:14:43";
$beginDate = strtotime($beginDate);
echo "beginDate ".date('Y-m-d h:i:s', $beginDate);
$beginDate3Day = strtotime("+3 day", $beginDate);
$beginDate3Days = date('Y-m-d h:i:s', $beginDate3Day);
echo "<br />$beginDate3Days ";
$today = date("Y-m-d h:i:s");
if (beginDate3Days > $today )
echo "It's been three days from..."
$beginDate = 2016-07-29 17:14:43 (this one is from sql table format)
$dateNow = date("Y-m-d H:i:s")
if (date('Y-m-d H:i:s', strtotime($beginDate.' +3 day')) >= $dateNow){
echo "3 days to be passed from begin date."
}else{
echo "3 days passed from begin date."
}

how to get date after 2 days from the given time in php

i want to find out the date after days from the given time.
for example. we have date 29 may 2015
and i want to cqlculate the date after 2 days of 25 may 2015
$Timestamp = 1432857600 (unix time of 29-05-2015)
i have tried to do it with following code but it is not working
$TotalTimeStamp = strtotime('2 days', $TimeStamp);
Missed the + - strtotime('2 days', $TimeStamp); .
Add the + to + 2 days.
Use date & strtotime for this - You can try this -
echo date('d-m-Y',strtotime(' + 2 day', strtotime('2015-05-16')));
$Timestamp & $TimeStamp are not same(may be typo). For your code -
$Timestamp = strtotime(date('Y-m-d'));
$TotalTimeStamp = strtotime('+ 2 days', $Timestamp);
echo date('d-m-Y', $TotalTimeStamp);
Php does have a pretty OOP Api to deal with date and time.
This will create a \DateTime instance using as reference the 25 May 2015 and then you can call the modify method on that instance to add 2 days.
$date = new \DateTime('2015-05-25');
$date->modify('+2 day');
echo $date->format('Y-m-d');
You may find this resource useful:
http://code.tutsplus.com/tutorials/dates-and-time-the-oop-way--net-35395
You can also just add seconds to your timestamp if you have a timestamp ready:
$NewDateStamp = $Timestamp + (60*60*24 * 2);
In the above, sec * min * hours = day -- or 86400 seconds. * 2 = 2 days.
In PHP 5 you can also use D
<?php
$date = date_create('2015-05-16');
date_add($date, date_interval_create_from_date_string('2 days'));
echo date_format($date, 'Y-m-d');
?>
OR
<?php
$date = new DateTime('2015-05-16');
$date->add(new DateInterval('2 days'));
echo $date->format('Y-m-d') . "\n";
?>

PHP mktime() is returning wrong date

im doing some stuff with mktime, i need to add the next date with 30 days more but its returning me 1970-01-30 date, what im doing wrong ?
$strtime=strtotime("2013-10-04");
$fecha=date("Y-m-d",$strtime);
echo $fecha."<br />";
$nueva_fecha=mktime(0,0,0,date("n",$fecha),date("j",$fecha)+30,date("Y",$fecha));
echo date("Y-m-d",$nueva_fecha)."<br />";
Result:
2013-10-04
1970-01-30
Date is looking for a timestamp as it's 2nd parameter, not a string value representing this. Updated to pass it $strtime instead.
$strtime=strtotime("2013-10-04");
$fecha=date("Y-m-d",$strtime); // <-- Unnecessary unless you want to echo the value.
echo $fecha."<br />";
$nueva_fecha=mktime(0,0,0,date("n",$strtime),date("j",$strtime)+30,date("Y",$strtime));
echo date("Y-m-d",$nueva_fecha)."<br />";
Output:
2013-10-04
2013-11-03
You can just use the following function to add 30 days to the date you put in:
$date = strtotime("2013-10-04");
$new_date = strtotime("+30 days", $date);
or simply to the current date:
$new_date = strototime("+30 days", time());
If you already have strtotime, why plus on date ? Instead you could've used + (30 days in seconds) OR simply the feature they offer you + 1 day check answer: adding one day to a date
strtotime('2013-10-04 + 30 days');
This will print 2013-11-03:
date('Y-m-d', strtotime('2013-10-04 + 30 days'))
you can try this:
echo strtotime("+1 day"), "\n";
echo strtotime("+30 day",strtotime(date('D, d M Y'))), "\n";
this will add 30 days to the current date.
Also strtotime is very usefull you can use it for weekly,monthly and yearly.
You can use this also
<?php
$date = date("Y/m/d"); // example date in yyyy/mm/dd format
$unix_time = strtotime( $date ); // covert date to unix time
$sec_in_30_days = 60 * 60 * 24 * 30; // 60 seconds * 60 minutes * 24 hours * 30 days
$new_unix_time = $unix_time + $sec_in_30_days; // add 30 days to unix time
$date_in_30_days = date( 'Y/m/d', $new_unix_time ); // convert new unix time to date
// Output results:
echo 'original current date: ' . $date . '<br />';
echo '<br />';
echo 'new date: ' . $date_in_30_days . '<br />';
?>
Output will be
original current date: 2013/10/04
new date: 2013/11/03

Adding days to $Date in PHP

I have a date returned as part of a MySQL query in the form 2010-09-17.
I would like to set the variables $Date2 to $Date5 as follows:
$Date2 = $Date + 1
$Date3 = $Date + 2
etc., so that it returns 2010-09-18, 2010-09-19, etc.
I have tried
date('Y-m-d', strtotime($Date. ' + 1 day'))
but this gives me the date before $Date.
What is the correct way to get my Dates in the format form 'Y-m-d' so that they may be used in another query?
All you have to do is use days instead of day like this:
<?php
$Date = "2010-09-17";
echo date('Y-m-d', strtotime($Date. ' + 1 days'));
echo date('Y-m-d', strtotime($Date. ' + 2 days'));
?>
And it outputs correctly:
2010-09-18
2010-09-19
If you're using PHP 5.3, you can use a DateTime object and its add method:
$Date1 = '2010-09-17';
$date = new DateTime($Date1);
$date->add(new DateInterval('P1D')); // P1D means a period of 1 day
$Date2 = $date->format('Y-m-d');
Take a look at the DateInterval constructor manual page to see how to construct other periods to add to your date (2 days would be 'P2D', 3 would be 'P3D', and so on).
Without PHP 5.3, you should be able to use strtotime the way you did it (I've tested it and it works in both 5.1.6 and 5.2.10):
$Date1 = '2010-09-17';
$Date2 = date('Y-m-d', strtotime($Date1 . " + 1 day"));
// var_dump($Date2) returns "2010-09-18"
From PHP 5.2 on you can use modify with a DateTime object:
http://php.net/manual/en/datetime.modify.php
$Date1 = '2010-09-17';
$date = new DateTime($Date1);
$date->modify('+1 day');
$Date2 = $date->format('Y-m-d');
Be careful when adding months... (and to a lesser extent, years)
Here is a small snippet to demonstrate the date modifications:
$date = date("Y-m-d");
//increment 2 days
$mod_date = strtotime($date."+ 2 days");
echo date("Y-m-d",$mod_date) . "\n";
//decrement 2 days
$mod_date = strtotime($date."- 2 days");
echo date("Y-m-d",$mod_date) . "\n";
//increment 1 month
$mod_date = strtotime($date."+ 1 months");
echo date("Y-m-d",$mod_date) . "\n";
//increment 1 year
$mod_date = strtotime($date."+ 1 years");
echo date("Y-m-d",$mod_date) . "\n";
You can also use the following format
strtotime("-3 days", time());
strtotime("+1 day", strtotime($date));
You can stack changes this way:
strtotime("+1 day", strtotime("+1 year", strtotime($date)));
Note the difference between this approach and the one in other answers: instead of concatenating the values +1 day and <timestamp>, you can just pass in the timestamp as the second parameter of strtotime.
Here has an easy way to solve this.
<?php
$date = "2015-11-17";
echo date('Y-m-d', strtotime($date. ' + 5 days'));
?>
Output will be:
2015-11-22
Solution has found from here - How to Add Days to Date in PHP
Using a variable for Number of days
$myDate = "2014-01-16";
$nDays = 16;
$newDate = strtotime($myDate . '+ '.$nDays.' days');
echo new Date('d/m/Y', $newDate); //format new date
Here is the simplest solution to your query
$date=date_create("2013-03-15"); // or your date string
date_add($date,date_interval_create_from_date_string("40 days"));// add number of days
echo date_format($date,"Y-m-d"); //set date format of the result
This works. You can use it for days, months, seconds and reformat the date as you require
public function reformatDate($date, $difference_str, $return_format)
{
return date($return_format, strtotime($date. ' ' . $difference_str));
}
Examples
echo $this->reformatDate('2021-10-8', '+ 15 minutes', 'Y-m-d H:i:s');
echo $this->reformatDate('2021-10-8', '+ 1 hour', 'Y-m-d H:i:s');
echo $this->reformatDate('2021-10-8', '+ 1 day', 'Y-m-d H:i:s');
To add a certain number of days to a date, use the following function.
function add_days_to_date($date1,$number_of_days){
/*
//$date1 is a string representing a date such as '2021-04-17 14:34:05'
//$date1 =date('Y-m-d H:i:s');
// function date without a secrod argument returns the current datetime as a string in the specified format
*/
$str =' + '. $number_of_days. ' days';
$date2= date('Y-m-d H:i:s', strtotime($date1. $str));
return $date2; //$date2 is a string
}//[end function]
All have to use bellow code:
$nday = time() + ( 24 * 60 * 60);
echo 'Now: '. date('Y-m-d') ."\n";
echo 'Next Day: '. date('Y-m-d', $nday) ."\n";
Another option is to convert your date string into a timestamp and then add the appropriate number of seconds to it.
$datetime_string = '2022-05-12 12:56:45';
$days_to_add = 1;
$new_timestamp = strtotime($datetime_string) + ($days_to_add * 60 * 60 * 24);
After which, you can use one of PHP's various date functions to turn the timestamp into a date object or format it into a human-readable string.
$new_datetime_string = date('Y-m-d H:i:s', $new_timestamp);

Categories