adding days with a date in php - php

In my project i have to get the expiry date of a package from the 'date of purchasing' the package and the 'validity in days' of the package.I got a code from the php.net as follows
<?php
$date = new DateTime('2000-01-01');
$date->add(new DateInterval('P10D'));
echo $date->format('Y-m-d') . "\n";
?>
Now suppose the date of purchase is like
2013-01-17
and the validity in days is
180
Now if i use the php.net example as follows
<?php
$date = new DateTime('2013-01-17');
$date->add(new DateInterval('P180D'));
echo $date->format('Y-m-d') . "\n";
?>
Will i get the output as 2013-04-17?
I need the output like
yyyy-mm-dd
I cant run the program in my personal computer but i have to carry on the codding of the project so i'm here.

<?php
$date = new DateTime('2013-01-17');
$date->add(new DateInterval('P180D'));
echo $date->format('Y-m-d') . "\n";
?>
//output
//2013-07-16 and it is in yyyy-mm-dd format
working example http://codepad.viper-7.com/gAtT2D
and if you can't run the program in your PC, use online debugger shell.

Related

create altered time record based on existing value

How do you take an existing 'date and time' value and convert it to the same date but a specified time?
For example, $time="2017-09-01 13:18:00" -> how to do you convert to "2017-09-01 23:59:59"? It must keep the date but change the time to 23:59:59.
You can do it like this by explode and simple concatenation
<?php
$time="2017-09-01 13:18:00";
$date = explode(" ", $time)[0];
echo $date." 23:59:59";
?>
Live demo : https://eval.in/853822
Update
I think you need this
<?php
$date = new DateTime('2017-09-01 13:18:00');
$date->setTime(23, 59,59);
echo $date->format('Y-m-d H:i:s') . "\n";
?>
Live demo : https://eval.in/853857
What about this?
$date = new DateTime('2017-09-01 13:18:00');
$date->add(new DateInterval('PT10H30S'));

PHP add time to date

I have a php time "09:00" that I want to set as the time for a date.
$date="2016-08-21 00:00:00.000000";
$time="09:00";
So ideally:
$datetime=$date+$time;//<----------what is the function for this
echo $datetime;
gives "2016-08-21 09:00:00.000000"
or
$date="2016-08-21 00:00:00.000000";
$time="17:30";
then
$datetime=$date+$time;//<----------what is the function for this
echo $datetime;
gives "2016-08-21 17:30:00.000000".
A seemingly simple requirement I cannot find a solution to.
try this :
<?php
$date = new DateTime('2000-01-01');
$date->add(new DateInterval('PT10H30S'));
echo $date->format('Y-m-d H:i:s') . "\n";
?>
Pending Mark adding an answer this was what I had but it seems clumsy - I hoped there was a better way:
$timebits = explode(":", $timepart);
$now->setTime($timebits[0], $timebits[1], $timebits[2]);

Calculate date using PHP

I am currently using the following PHP string which collects the due date from an invoice in the format DD.MM.YYYY.
<?php echo date_from_mysql($invoice->invoice_date_due , TRUE); ?>
I would like to automatically calculate the date five days prior to this, and if possible, have it display in full (eg. Monday, 1st January 2016)
Any help on this would be appreciated!
You can use DateTime for this, and the sub() function.
$objDate = new \DateTime(date_from_mysql($invoice->invoice_date_due , TRUE), new \DateTimeZone('Europe/London'));
$objDate->sub(new DateInterval('P5D')); //minus 5 days
echo $objDate->format('Y-m-d H:i:s');
https://eval.in/512035
Try this for your required date formation
<?php
$date = new DateTime('01.02.2016'); //modify if you wish
$date->sub(new DateInterval('P5D')); //calculate the date five days prior
echo $date->format('l, jS F Y') . "\n"; //formatted as you mentioned
?>

PHP rename with date stamp

we want to make a script that renames the file name with a date stamp, we're new to PHP so this is what we have now.
<?php rename("test.txt", "tes3.txt"); ?>
we tried some with things like this below but we don't get it to work.
$date = new DateTime();
echo $date->format('Y-m-d H:i:sP') . "\n";
I hope you guys have a answer how we can do this.
Thanks
All you have to do is use the textual output (string) produced by $date->format(...) and inject the answer in the rename call.
$date = new DateTime();
rename("test.txt", "test" . $date->format('Y-m-d H:i:sP') . ".txt");
But it is advisable to not use any spaces in filenames, especially if you want the files to be accessible through a server/website/...

How would I incorporate DATEJS into a form driven by PHP?

I'm creating a simple app (PHP & MYSQL) that collects information about an event, and I'd like to incorporate natural language processing in the date collection portion. I stumbled upon date.js (www.datejs.com), and it seems like a great fit, but I'm not sure how I'd incorporate it into the input form and then pass the converted string as a variable to the database.
Has anybody successfully implemented this in a PHP environment (or have a better solution)?
You can do the same with the php function [strtotime][1]
<?php
echo strtotime("now"), "\n";
echo strtotime("10 September 2000"), "\n";
echo strtotime("+1 day"), "\n";
echo strtotime("+1 week"), "\n";
echo strtotime("+1 week 2 days 4 hours 2 seconds"), "\n";
echo strtotime("next Thursday"), "\n";
echo strtotime("last Monday"), "\n";
?>
[1]: http://www.php.net/manual/en/function.strtotime.php strtotime

Categories