php - issue with date-format - php

I am struggeling with some basic date-formatting and echo the right number for "Days Left". This is the code:
date_default_timezone_set('Europe/Oslo');
$dateNow = date('d/m/Y', time());
$dateStart = date('d/m/Y', strtotime('01/08/2014'));
$dateFinished = date('d-m-Y', $dateStart + strtotime("+62 days"));
$daysLeft = intval($dateFinished-$dateNow);
echo($dateNow. "<br>");
echo($dateStart. "<br>");
echo($daysLeft. "<br>");
This is the echo:
07/07/2014
01/08/2014
-6
It seems like dateStart is interped as a wrong format. I have tried with both - and /, but nothing better.
What have i done wrong? Just the math or is it the logic?

I recommend to use OOP (Object-oriented) in this case. It is easier to write and easier to understand.
Just made a simple and short script:
<?php
date_default_timezone_set('Europe/Oslo');
$dateNow = new DateTime('now');
$dateStart = new DateTime('2014-08-01');
$dateFinished = new DateTime($dateStart->format('Y-m-d'));
$dateFinished->add(new DateInterval('P62D'));
$daysLeft = $dateFinished->diff($dateNow);
echo($dateNow->format('d/m/Y'). "<br>");
echo($dateStart->format('d/m/Y'). "<br>");
echo($daysLeft->format('%d'). "<br>");
?>

$dateStart is a string. You need to do:
$dateStart = date('d/m/Y', strtotime('01/08/2014'));
$dateFinished = date('d-m-Y', strtotime("+62 days", strtotime('01/08/2014')));
For more sophisticated date calculations consider DateTime().

If your PHP is 5.3+, than you can use DateTime class and calculate difference between dates:
$date1 = new DateTime("2007-03-24");
$date2 = new DateTime("2009-06-26");
$interval = $date1->diff($date2);
echo "difference " . $interval->y . " years, " . $interval->m." months, ".$interval->d." days ";
OR
echo "difference " . $interval->days . " days ";

Related

Getting the Date and numeric weekday in PHP

i'm developing an application in PHP and I need to use dates and the numeric representation of weekdays.
I've tried the following:
$today = date("Y-m-d");
$number = date('N', strtotime($today));
echo "Today: " . $today . " weekday: " . $number . "<br>";
$today = strtotime($today);
$tomorrow = strtotime($today);
$tomorrow = strtotime("+1 day", $today);
$number2 = date('N', strtotime($tomorrow));
echo "Tomorrow: " . date('Y-m-d', $tomorrow) . " weekday: " . $number2 . "<br>";
Output
Today: 2016-11-11 weekday: 5
Tomorrow: 2016-11-12 weekday: 4
This isn't right because the weekday of tomorrow should be 6 instead of 4.
can someone help me out?
Using DateTime would provide a simple solution
<?php
$date = new DateTime();
echo 'Today: '.$date->format( 'Y-m-d' ) .' weekday '. $date->format( 'N' )."\n";
$date->modify( '+1 days' );
echo 'Tomorrow: '.$date->format( 'Y-m-d' ) .' weekday '. $date->format( 'N' )."\n";
Output
Today: 2016-11-11 weekday 5
Tomorrow: 2016-11-12 weekday 6
However the day numbers are slightly different, the N respresents the weekday number and as you can see Friday (Today) is shown as 5. With that Monday would be 1 and Sunday would be 7.
If you look at the example below you should get the same result
echo date( 'N' );
Output
5
Date Formatting - http://php.net/manual/en/function.date.php
You have little error in the code, here`s the working one:
$today = date("Y-m-d");
$number = date('N', strtotime($today));
echo "Today: " . $today . " weekday: " . $number . "<br>";
$today = strtotime($today);
$tomorrow = strtotime($today);
$tomorrow = strtotime("+1 day", $today);
$number2 = date('N', $tomorrow);
echo "Tomorrow: " . date('Y-m-d', $tomorrow) . " weekday: " . $number2 . "<br>";
DateTime is the Object Oriented method of working with dates in PHP. I find it to be working much more fluently. That aside, it looks alot better.
// Create a new instance
$now = new DateTime();
echo $now->format('N');
// Next day
$now->modify('+1 day');
echo $now->format('N');
Resources
DateTime manual - PHP.net
You almost have it right but not quite. Why are you using strtotime on $number2? Change it to $number2 = date('N', $tomorrow); and it will work.

PHP Time String Difference

I need help with my brand new experience with PHP coding!
I have two time strings with the following format:
hh:mm:ss yyyy-mm-dd
find two examples down below:
10:35:56 2013-11-15
11:46:24 2013-11-16
I need to get the difference between the second and the first one (using some PHP function or line of code) in some way that could be reused to compute the same difference of other rows in my DB on demand...
$start=strtotime("10:35:56 2013-11-15");
$end=strtotime("11:46:24 2013-11-16");
$difference=$end-$start;
echo $difference. " Seconds";
use strtotime()
$date1 = strtotime('10:35:56 2013-11-15');
$date2 = strtotime('11:46:24 2013-11-16');
echo $date2 - $date1 . 'seconds';
You can do it this way with strtotime :
$date1 = strtotime("10:35:56 2013-11-15");
$date2 = strtotime("11:46:24 2013-11-16");
$date_diff = $date2 - $date1; // here you got difference in seconds.
echo $date_diff . " seconds";
Or with DateTime object :
$datetime1 = date_create('10:35:56 2013-11-15');
$datetime2 = date_create('11:46:24 2013-11-16');
$date_diff = date_diff($datetime1, $datetime2);
echo $date_diff ->format('%s seconds');
The $date_diff -> format(...) uses this list of format.
For exact difference try this
$datetime1 = new DateTime('10:35:56 2013-11-15');
$datetime2 = new DateTime('11:46:24 2013-11-16');
$interval = $datetime1->diff($datetime2);
echo $interval->m . " Month " .$interval->d ." Days ". $interval->h . " Hours, " . $interval->i." Mintues, ".$interval->s." seconds <br/>";

if current_time is 1 day over logged_time php [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
adding one day to a date
I'm trying to add a day to a value pulled from a mysql row.
so the value getting returned is let's say
2012-10-22 22:12:13
and I want to make it
2012-11-22 22:12:13
and store it in the variable without having to interval it back into mysql and then pull it right back out.
i tried doing
$end_date_add = $enddate + 0000 . "-" . 00 . "-" . 01 . " " . 00 . ":" . 00 . ":" . 00;
with $end_date being the time logged, but it replaces the time with zeros.
am I going about this wrong?
Any help much appreciated, thank you.
This is what you want, i guess...
$date_old = strtotime("+1 MONTH", strtotime("2012-10-22 22:12:13"));
echo date("Y-m-d H:i:s", $date_old);
You can make use of strtotime to add the one month period:
$date = '2012-10-22 22:12:13';
$format = 'Y-m-d H:i:s';
echo date($format, strtotime("$date +1 MONTH"));
Output (Demo):
2012-11-22 22:12:13
You can also make use of PHP's DateTime type and add a DateInterval of one day:
$date = '2012-10-22 22:12:13';
$format = 'Y-m-d H:i:s';
echo (new DateTime($date))->add(new DateInterval('P1M'))->format($format);
Output (Demo):
2012-11-22 22:12:13
The code above is PHP 5.4, in PHP 5.3 you can do:
echo date_add(new DateTime($date), new DateInterval('P1M'))->format($format);
Date adding
$date = date("Y-m-d"); // current date
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 day");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 week");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +2 week");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 month");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +30 days");
This could also be done as part of the MYSQL query
SELECT DATE_ADD(datecol, INTERVAL 1 DAY) AS 'datecol_plus_one'

Add days to a date in PHP

Is there any php function available where I can add days to a date to make up another date? For example, I have a date in the following format:
27-December-2011
If I add 7 to the above, it should give:
03-January-2012.
Many thanks
Try this
$add_days = 7;
$date = date('Y-m-d',strtotime($date) + (24*3600*$add_days));
Look at this simple snippet
$date = date("Y-m-d");// current date
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 day");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 week");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +2 week");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 month");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +30 days");
You can use the add method of DateTime. Anyway this solution works for php version >= 5.3
date('Y-m-d', strtotime('+6 days', strtotime($original_date)));
Actually it's easier than all that.
$some_var = date("Y-m-d",strtotime("+7 day"))
You can use a variable instead of the string, of course. It will be great if the people answering the questions, won't complicate things. Less code, means less time to waste on the server ;).
$date = new DateTime('27-December-2011');
$date->add(new DateInterval('P7D'));
echo $date->format('d-F-Y') . "\n";
Change the format string to be whatever you want. (See the documentation for date()).
$registered = $udata->user_registered;
$registered = date( "d m Y", strtotime( $registered ));
$challanexpiry = explode(' ', $registered);
$day = $challanexpiry[0];
$month = $challanexpiry[1];
$year = $challanexpiry[2];
$day = $day+10;
$bankchallanexpiry = $day . " " . $month . " " . $year;

Get the date of one week from today

How do I get the date, one week from today, in the following format: YYYY-MM-DD ?
Try:
date("Y-m-d", strtotime("+1 week"));
This will output:
2015-12-31
If today is 2015-12-24
Just so Charles' prediction is wrong, here's a PHP 5.3+ example:
$now = new DateTime;
$interval = new DateInterval('P1W')
$next_week = $now->add($interval);
echo $next_week->format('Y-m-d');
or in slightly more compact form:
$now = new DateTime();
echo $now->add(new DateInterval('P1W'))->format('Y-m-d');
adding days, weeks, months to any date
$date = date("Y-m-d");// current date
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 day");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 week");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +2 week");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 month");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +30 days");
<?php
$nextWeek = time() + (7 * 24 * 60 * 60);
echo 'Next Week: '. date('Y-m-d', $nextWeek) ."\n";
One missing from Charles prediction, straight from the horses mouth, example #1
for ($i=0 ; $i < 7 ;$i++)
{
$date[]=date('Y-m-d',strtotime("+{$i} day",time()));
}
print_r($date);

Categories