Add 23 hrs 59 minutes as a unix time stamp in PHP - php

Can someone give me a working example of what my code should be as my efforts are getting me nowhere?
My code currently says have
if (isset($_POST['startdate'])) {
$startdate = DatePickerToTime($_POST['startdate']);
} else {
$startdate = strtotime('today midnight');
}
and
if (isset($_POST['enddate'])) {
$enddate = DatePickerToTime($_POST['enddate']);
} else {
$enddate = time();
}
which works.
However, I want to say if startdate is equal to enddate then add 23 hours and 59 minutes to enddate
I have read the documentation and am trying
if($_POST['$startdate'] == $_POST['$enddate']) {
$_POST['$enddate'] += date("h:i:sa", "23:59:59");
}
but this doesn't work.

$datetime1 = date('2009-10-11 12:30:00');
$datetime2 = date('2009-10-11 12:30:00');
echo "<pre>";
if($datetime1 == $datetime2){
echo "yes";
echo "<br>";
$datetime2 = date("Y-m-d H:i:s", strtotime("+23 hours 59 minutes",strtotime($datetime2)));
echo $datetime1."===================".$datetime2;
}
else {
echo "NO<br>";
echo $datetime1."===================".$datetime2;
}

Related

How to convert time duration in PT format to regular string in php

I have data like following
PT45M
PT1H30M
How can i get an output like
45 min
1 hour 30 min
Is there any function in php to get output like shown?
$dur="PT45M";
$d = new DateInterval($dur);
$duration=$d->format('%h hour %m min');
$duration=str_replace(" 0 min", "",$duration );
$duration=str_replace("0 hour", "",$duration );
This is one solution almost worked for me. But on PT45M it is showing 0 hour. :(
It may be a simple function, but am unable to find a solution. Have spent a lot of time in googleing :( Hope someone can help me with this.
The solution is given below. I was using %m instead of using %i for minutes. (%m is for numeric month)
Here is the documentaion : https://www.php.net/manual/en/function.date.php
$dur="PT45M";
$d = new DateInterval($dur);
$duration=$d->format('%h hour %i min');
$duration=str_replace(" 0 min", "",$duration );
$duration=str_replace("0 hour", "",$duration );
A function that creates a human-readable format from a DateInterval object:
function dateIntervalToHumanString(\DateInterval $interval) {
$units = array("y"=>"year","m"=>"month","d"=>"day","h"=>"hour","i"=>"minute","s"=>"second");
$specString = "";
foreach($units as $prop => $spec){
if($number=$interval->$prop){
$specString .= $number." ".$spec;
$specString .= $number > 1 ? "s " : " ";
}
}
return trim($specString);
}
How to use it?
$specStr = 'P2Y4DT1H8M';
$di = new DateInterval($specStr);
$humanString = dateIntervalToHumanString($di);
echo $humanString; //2 years 4 days 1 hour 8 minutes
<?php
$date = new DateTime();
$dur="PT1H30M";
$datetime1 = new DateInterval($dur);
if($datetime1->format('%y') != 0)
{
echo $datetime1->format('%y Year');
}
if($datetime1->format('%m') != 0)
{
echo $datetime1->format('%m Month');
}
if($datetime1->format('%d') != 0)
{
echo $datetime1->format('%d Day');
}
if($datetime1->format('%h') != 0)
{
echo $datetime1->format('%h Hour');
}
if($datetime1->format('%i') != 0)
{
echo $datetime1->format('%i Min');
}
if($datetime1->format('%s') != 0)
{
echo $datetime1->format('%s Second');
}
?>

How to find out the number of days between two dates

i am trying to get number of days between two given dates, but while trying this way its not giving the number of days.
$pur_dt = date_create('2015-08-03');
$todate = date_create(date('Y-m-d'));
$diff = date_diff($todate,$pur_dt);
print_r($diff);
echo $diff->format('%R%a days');
if($diff>15) //checking condition if $pur_dt - $todate > 15
{
echo 'Hello you are not eligible';
}
else
{
echo 'eligible';
}
its not working, not giving the number of days between given two dates.
Try this. It is very simple.
<?php
$date1 = strtotime("2015-11-16 10:01:13");
$date2 = strtotime("2015-05-06 09:47:16");
$datediff = $date1 - $date2;
echo floor($datediff/(60*60*24))." days"; //output 194 days
?>
It's better using DateTime class, you can see comment(9) at PHP manual as it answer your question
Try this,
$pur_dt = date_create('2015-08-03');
$todate = date_create(date('Y-m-d'));
$datediff = $pur_dt - $todate;
$diff = $datediff/(60*60*24);
if($diff>15) //checking condition if $pur_dt - $todate > 15
{
echo 'Hello you are not eligible';
}
else
{
echo 'eligible';
}
Try This :
$pur_dt = Date('2015-08-03');
$todate = Date(date('Y-m-d'));
$pur_dt = strtotime($pur_dt);
$todate = strtotime($todate);
$seconds_diff = $todate - $pur_dt;
$$diff = floor($seconds_diff/(60*60*24));
if($diff>15) //checking condition if $pur_dt - $todate > 15
{
echo 'Hello you are not eligible';
}
else
{
echo 'eligible';
}
Try this
$pur_dt = date_create('2015-08-03');
$todate = date_create(date('Y-m-d'));
$diff = date_diff($todate,$pur_dt);
print_r($diff);
echo $diff->format('%R%a days');
if($diff->days>15) //checking condition if $pur_dt - $todate > 15
{
echo 'Hello you are not eligible';
}
else
{
echo 'eligible';
}

How to check if given timestamp is 10 minutes old?

I have a timestamp in '2013-04-10T09:00:00Z' format and based on this I want to check the requested datetime format is within 10 minutes or not.
Try this:
$now = time();
$timestamp = strtotime('2013/04/08T09:00:00Z');
$timediff = $now - $timestamp;
if (floor($timediff/60) > 10) {
echo 'Time is 10 minutes older';
}
else {
echo 'Time is not older then 10 min';
}
Below is the code that I tried and it worked for me,
$currentDateTime =new DateTime();
$currentDateTime->setTimezone(new DateTimeZone('UTC'));
$userDateTime = new DateTime($timestamp); //convert users incoming
$diff = $now->diff($then);
$minutes = ($diff->format('%a')*1440)+($diff->format('%h')*60)+ ($diff->format('%i'));
if($minutes <=10)
{
//your code goes here
}
else
{
//time difference is more then 10 minutes<br/>
}
Thanks Francisco for all your post.

time passed more than 1 week and less than 2 week

is it possible to do something like this.
i was trying to do it but couldn't...
this is what i was trying to do
$date2second = strtotime('2013-03-5');
$date1week = strtotime('2013-03-5') + 604800;
//passed less than
//$datetillnextweek = strtotime('2013-03-5') + 1209600;
$datetillnextweek = strtotime(date('Y-m-d')) + 1209600;
echo "$date2second <br>";
echo "$date1week <br>";
echo "$datetillnextweek <br>";
if($date2second < $date1week && $date2second <= $datetillnextweek)
{
echo "action";
}
$now = new DateTime('2013-03-28');
$one_week = new DateTime();
$one_week->modify('+1 week');
$two_weeks = new DateTime();
$two_weeks->modify('+2 weeks');
if ($now > $one_week && $now < $two_weeks)
{
// you're here
}
See it in action
check the date add function and other date class options. you can specify the interval and the format to present it .
http://www.php.net/manual/en/datetime.add.php
<?php
$date = new DateTime('2000-01-01');
$date->add(new DateInterval('P7D'));
echo $date->format('Y-m-d') . "\n";
?>
To see if its in a time period you can also use http://www.php.net/manual/en/datetime.diff.php to see if the difference iis larger then 7 days

How to make a check if this timestamp is today,tomorrow or the day after tomorrow?

I would like to know how to check if the timestamp is today, tomorrow or the day after tomorrow.
I have e.g. :
$timestamp = "1313000474";
How to make a check if this timestamp is today,tomorrow or the day after tomorrow?
e.g.
if today then echo $output = "today";
if tomorrow then echo $output = "tomorrow";
if the day after tomorrow then echo $output = "dayaftertomorrow";
How to do this?
EDIT: corrected unix timestamp
Thank you in advance.
$timestamp = "1313000474";
$date = date('Y-m-d', $timestamp);
$today = date('Y-m-d');
$tomorrow = date('Y-m-d', strtotime('tomorrow'));
$day_after_tomorrow = date('Y-m-d', strtotime('tomorrow + 1 day'));
if ($date == $today) {
echo "today";
} else if ($date == $tomorrow) {
echo "tomorrow";
} else if ($date == $day_after_tomorrow) {
echo "dayaftertomorrow";
}
Keep your code clean...
$timestamp = "1313000474";
// Description demonstrate proposes only...
$compare_dates = array(
'today' => 'today!!',
'tomorrow' => 'Tomorrow!!!',
'tomorrow +1 day' => 'day after tomorrow? YEAH',
);
foreach($compare_dates => $compare_date => $compare_date_desc){
if(strtotime($compare_date) > $timestamp && $timestamp < strtotime($compare_date.' +1 day') ){
echo $compare_date_desc;
break;
}
}
EDIT: With this you dont have to worry if the timestamp is already without hours, minutes and seconds... Or create different output dates, replacing echo $compare_date_desc; by echo date($compare_date_desc,$timestamp);
<?php
$time = "20060713174545";
$date = date('Y-m-d', strtotime($time));
$now = date('Y-m-d');
$tomorrow = date('Y-m-d', time() + strtotime('tomorrow'));
$day_after_tomorrow = date('Y-m-d', time() + strtotime('tomorrow + 1 day'));
if ($date == $now){
echo "It's today";
}
elseif($date == $tomorrow){
echo "It's tomorrow";
}
elseif($date == $day_after_tomorrow){
echo "It's day after tomorrow";
}
else{
echo "None of previous if statements passed";
}
<?php
function getTheDay($date)
{
$curr_date=strtotime(date("Y-m-d H:i:s"));
$the_date=strtotime($date);
$diff=floor(($curr_date-$the_date)/(60*60*24));
switch($diff)
{
case 0:
return "Today";
break;
case 1:
return "Yesterday";
break;
default:
return $diff." Days ago";
}
}
?>

Categories