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
?>
Related
I have a variable is which the value coming is Date along with time in php. How do I convert it into a variable to get only the year? I do not need automatic updation but the format change is needed. Normal answers are giving it about date but my variable is containing time as well.
The format coming by now is 2017-12-11 4:06:37 and i need only 2017
Use like this:
<?php echo date('Y',strtotime('now'));?>
You can you simple DateTime function and date_formate() function for displaying separate year, month and date.
For that you have to first convert in Object of your current Date time string by using :
$date = new \DateTime('2017-12-11 4:06:37');
And then you can use date format function by using below code:
echo date_format($date, "Y"); //for Display Year
echo date_format($date, "m"); //for Display Month
echo date_format($date, "d"); //for Display Date
You can code like this (working perfectly):
$format = 'Y-m-d H:i:s';
$date = DateTime::createFromFormat($format, '2009-02-15 15:16:17');
echo "Format: $format; " . $date->format('Y') . "\n";
As mentioned by Himanshu Upadhyay, this is correct and the easiest way.
<?php
echo date('Y',strtotime('now'));
?>
But i would recommend you to read this here. You should really do actually!
By using DateTime class
$date = new \DateTime('2017-12-11 4:06:37');
echo $date->format('Y');
$date_raw = '05/05/1995';
$newDate = (date('j F Y', strtotime('-192years -14months -2days', strtotime($date_raw))));
print "New Date: $newDate <br>";
I'm trying to subtract 100+ years from a given date. but the value i get is real till 92 years only. after that i don't get correct subtraction. whats the reason?
If you need to work with dates that fall outside the range of a 32-bit signed unix timestamp (1901-12-13 to 2038-01-19), then start using DateTime objects
$date_raw = '05/05/1995';
$newDate = (new DateTime($date_raw))
->sub(new DateInterval('P192Y14M2D'))
->format('j F Y');
echo $newDate, PHP_EOL;
gives
3 March 1802
or you can do this in the following way
// set your date here
$mydate = "2018-06-27";
/* strtotime accepts two parameters.
The first parameter tells what it should compute.
The second parameter defines what source date it should use. */
$lastHundredyear = strtotime("-100 year", strtotime($mydate));
// format and display the computed date
echo date("Y-m-d", $lastHundredyear);
this will give you following output
1918-06-27
I've following small code snippet to display today's date and value of one variable but I'm getting the blank white page in result. Why so?
My code is as below:
<?php
$form_data['trans_date'] = '12-11-2014';// Date format is MM-DD-YYYY i.e. 11th December 2014
$newTransDate = DateTime::createFromFormat('!m-d-Y', $form_data['trans_date']);
$today_date = new DateTime('today');
echo "Today Date ".$today_date; die;//Here I want to print today's date
$form_data['trans_date'] = '12-11-2014';
$newTransDate = DateTime::createFromFormat('!m-d-Y', $form_data['trans_date']);
$today_date = new DateTime('today');
echo "Today Date ".$newTransDate."<br>"; die;//Here I wan tot print today's date
echo "New Trans Date ".$newTransDate; die;//Here I wan tot print value of var $newTransDate
?>
Thanks in advance.
When you are using PHP's DateTime, while outputting you need to define the output format.
// Create a new DateTime object
$now = new DateTime();
// Output
echo $now->format('Y-m-d H:i:s');
I have a PHP date in a database, for example 8th August 2011. I have this date in a strtotime() format so I can display it as I please.
I need to adjust this date to make it 8th August 2013 (current year). What is the best way of doing this? So far, I've been racking my brains but to no avail.
Some of the answers you have so far have missed the point that you want to update any given date to the current year and have concentrated on turning 2011 into 2013, excluding the accepted answer. However, I feel that examples using the DateTime classes are always of use in these cases.
The accepted answer will result in a Notice:-
Notice: A non well formed numeric value encountered......
if your supplied date is the 29th February on a Leapyear, although it should still give the correct result.
Here is a generic function that will take any valid date and return the same date in the current year:-
/**
* #param String $dateString
* #return DateTime
*/
function updateDate($dateString){
$suppliedDate = new \DateTime($dateString);
$currentYear = (int)(new \DateTime())->format('Y');
return (new \DateTime())->setDate($currentYear, (int)$suppliedDate->format('m'), (int)$suppliedDate->format('d'));
}
For example:-
var_dump(updateDate('8th August 2011'));
See it working here and see the PHP manual for more information on the DateTime classes.
You don't say how you want to use the updated date, but DateTime is flexible enough to allow you to do with it as you wish. I would draw your attention to the DateTime::format() method as being particularly useful.
strtotime( date( 'd M ', $originaleDate ) . date( 'Y' ) );
This takes the day and month of the original time, adds the current year, and converts it to the new date.
You can also add the amount of seconds you want to add to the original timestamp. For 2 years this would be 63 113 852 seconds.
You could retrieve the timestamp of the same date two years later with strtotime() first parameter and then convert it in the format you want to display.
<?php
$date = "11/08/2011";
$time = strtotime($date);
$time_future = strtotime("+2 years", $time);
$future = date("d/m/Y", $time_future);
echo "NEW DATE : " . $future;
?>
You can for instance output it like this:
date('2013-m-d', strtotime($myTime))
Just like that... or use
$year = date('Y');
$myMonthDay = date('m-d', strtotime($myTime));
echo $year . '-' . $myMonthDay;
Use the date modify function Like this
$date = new DateTime('2011-08-08');
$date->modify('+2 years');
echo $date->format('Y-m-d') . "\n";
//will give "2013-08-08"
I need to be able to calculate a date using PHP that displays the next 1st of June. So, today is 15th April 2013 therefore I need to display 01/06/2013 (UK format). If the date was 5th August 2013 I would need to display 01/06/2014.
Can anyone help?
Thanks,
John
You can achieve this using :
$now = time();
$june = strtotime("1st June");
if ($now > $june)
echo date("d/m/Y", strtotime('+1 year', $june));
else
echo date("d/m/Y", $june);
Hope this helps :)
For this you can achieve by checking the present month
if(date('m')>06)
{
$date= date('d-m-Y',strtotime("next year June 1st"));
}
else{
$date= date('d-m-Y',strtotime("this year June 1st"));
}
echo $date;
Create a new DateTime object for the current year. DateTime is the preferred way to handle dates in PHP.
If it's too early, create a new datetime object for the following year.
Finally, use 'format' to output.
$d = new DateTime(date('Y').'-08-05');
if ($d < new DateTime()) {
$d = new DateTime((date('Y')+1).'-04-15');
}
echo $d->format('d/m/Y');
You can achieve this using this tutorial. You can define time zone and display the date as per your format.
Check this manual. http://php.net/manual/en/function.date.php
clear examples are given here:
<?php
// set the default timezone to use. Available since PHP 5.1
date_default_timezone_set('UTC');
echo $today = date("d/m/y"); // 03/10/01
?>