I am trying to get only the month and year on this format (y-m) coming from y-m-d format of date.
I am getting an error saying Call to a member function format() on a non-object.
Am i doing it correctly? Thank you for your help with this.
$current_date= '2017-04-03';
$d = DateTime::createFromFormat("Y-m",$current_date);
$current_date = $d->format("Y-m");
echo $current_date;
My apologies, my bad, the reason for the error was I put the following code inside the for loop which causes error.
After moving the code outside the loop, the only thing I changed was the code from
$d = DateTime::createFromFormat("Y-m",$current_date);
to
$d = DateTime::createFromFormat("Y-m-d",$current_date);
Thanks alot!
Corrected code:
$current_date= '2017-04-03';
$d = DateTime::createFromFormat("Y-m-d",$current_date);
$current_date = $d->format("Y-m");
echo $current_date;
Related
I am trying to use the PHP date_create_from_format on an object so I can use the diff() function to calculate the number of days from when the record was created to today (whenever that is looked at)
If I echo $job->created_date I get something like 2021-2-27 10:05:00
I started with:
$created = $job->created_date;
$date_now = date('Y-m-d');
$job_posted_date_difference = $date_now->diff($created); <<< the error shows on this line
echo $job_posted_date_difference.$difference->d.' days';
but I get a fatal error Call to a member function diff() on string
So....... I thought that it could be a formatting of the initial date issue and then I tried
$created = date_create_from_format('Y-m-d', $job->created_date);
$date_now = date('Y-m-d');
var_dump ($created);
But the dump shows - bool(false)
How do I fix this properly? I looked at a number of other SO issues and they seemed to really range from the dates being in the wrong order( I tried both ways) to the formatting issue .
and if you want to calculate days from two dates
then you can use
$ceated_date = "2021-2-27 10:05:00";
//create a date object of created date and get timestamp.
$t1 = (new DateTime($created_date))->getTimetamp();
//create a date object of now and get timestamp.
$t2 = (new DateTime("now"))->getTimestamp();
//this will give you days
$day_diff = floor(($t2 - $t1) / 86400);
I have this php code that I want to output the current year, month and day.
date_default_timezone_set('Sweden/Stockholm');
$time_info = getdate();
$day = $time_info['mday'];
$month = $time_info["mon"];
$year = $time_info['year'];
$date = "$year, $month, $day";
And the output:
2014 10 29
But I want it to output
year-month-day
But when I change the $date to $date = $year."-".$month."-".$day."-"; the output is: 1975.
Obviously, this is wrong, so how can I fix it. And a explanation why this occurs would also be great.
EDIT:
Ok, so according to #Marc B and #Barry , it did at some point math, and they were right. I don't know why this occurs, but I got it sorted out. Thanks!
Why don't you use the php date function?
echo date("Y-m-d");
You can get that if you run this.
$time_info = getdate();
echo $time_info->format('Y-m-d');
Using PHP format() function resolves your problem.
Documentation is here.
use php date function Use Like this
date_default_timezone_set('Sweden/Stockholm');
echo date("Y-m-d");
I would like to fill a date in as dd-mm-yyyy but I would like to save the date as Y-m-d in my database. What I have is this:
Form:
$factuurDatum = new Zend_Form_Element_Text('datum');
Controller:
$data = $addInkoopfactuur->getValues();
$data['datum'] = date("Y-m-d", strtotime($data['datum']));
But it doesn't work. I still get an error: '08-01-2014' and does not fit the date format 'yyyy-MM-dd'. How can I resolve this problem?
Use this DateTime class
<?php
$pubDt='07-01-2014';
$date = DateTime::createFromFormat('d-m-Y', $pubDt);
echo $newPubdate = $date->format('Y-m-d');
DEMO : https://eval.in/86800
Did you want it?
$data['datum'] = "15-10-2013";
$data['datum'] = date("Y-m-d", strtotime($data['datum']));
echo $data['datum'] ;
OUTPUT:
2013-10-15
Got the problem myself! the addvalidator('Date', true) should be deleted in my form! Because the validator will set my date as yyyy-mm-dd. My question was not answered clear enough, sorry!
I am trying to get the next year date from a php variable which holds the date value posted from a html form.
Below is the code
$warranty_from = mysql_real_escape_string($_POST['from_date']);
Say the $warranty_from holds the value 2013-06-04. I want to get the next year date i.e
2014-06-04 and store into $warranty_to variable.
Searched lot over the net but couldnt find any resource related to my issue. Any help is appreciated. Thanks in advance.
The following should work:
$warranty_from = '2013-06-04';
$warranty_to = date('y-m-d', strtotime('+1 years', strtotime($warranty_from)));
Something like that should work
This was exact code
$newWarranty_date = date("Y-m-d",strtotime("+1 year",strtotime($warranty_from)));
Try this one
<?php
$warranty_to = date('Y-m-d', strtotime('+1 year', strtotime($warranty_from)));
?>
Calculations like this are trivial using the DateTime classes:-
$_POST['from_date'] = '2013-06-04';//Just for this example.
$warranty = DateTime::createFromFormat('Y-m-d', $_POST['from_date']);
$interval = new DateInterval('P1Y');
$warranty->add($interval);
echo $warranty->format('Y-m-d');
Will output:-
2014-06-04
I am trying to make for loops using date function but I can't. In my case, I am trying to loop 2001-feb to 2009-jan and want to insert date but I can't. Can anyone help me? Here is my code:
for($start_year; $start_year<= $end_year; $start_year++)
{
for($start_month; $start_month<= $end_month; $start_month++)
{
$date_input = $start_year."-".$start_month."-27";
}
}
Looks like PHP.
for($start_year; $start_year<= $end_year; $start_year++)
You've not specified a starting value for $start_year, so unless you've defined that variable elsewhere, this is a syntax error. As well, since you're starting/ending on different months, you can't really use a loop for that without jumping through hoops. You'd probably be better off with something like this:
$date = strtotime("2001-02-01 00:00:00");
$end = strtotime("2009-01-01 00:00:00");
do {
echo date('Y-m', $date), "-27";
$date = strtotime("+1 month", $date);
} while ($date <= $end);
Note that strtotime is only one way of doing this. You could use the DateTime object with a 1-month DateInterval, but that's for PHP 5.3+ only.