how to get "Year-Month" from "2014-11-01"
i can get those with following function
$date = "2014-11-01";
$year = date('Y', strtotime($date));
$month = date('F', strtotime($date));
echo $year.'-'.$month;//return 2014-11
OR
echo substr($this->getValue('PayMonth'),0,7);// return 2014-11
Now My question is can we do this using date() or any other PHP build-in function in one line
for example
echo date('Y-m','2014-11-01');
of course. Try this
echo date('Y-m',strtotime('2014-11-01'));
Try with strtotime
echo date('Y-m',strtotime('2014-11-01'));
You can also do with explode like
$date_array = explode('-','2014-11-01');
echo $date_array[0].'-'.$date_array[1];
But make sure that your date format will be like "YY-MM-DD", if you want to use explode.
Related
I have a date 01/31/2014, and I need to add a year to it and make it 01/31/2015. I am using
$xdate1 = 01/31/2014;
$xpire = strtotime(date("m/d/Y", strtotime($xdate1)) . " +1 year");
But it is returning 31474800.
Waaaay too complicated. You're doing multiple date<->string conversions, when
php > $x = strtotime('01/31/2014 +1 year');
php > echo date('m/d/Y', $x);
01/31/2015
would do the trick.
Another way is below:
<?php
$date = new DateTime('2014-01-31');
$date->add(new DateInterval('P01Y'));
echo $date->getTimestamp();
?>
There are 2 mistakes here.
You are missing the quote sign " when assigning to $xdate1. It should be
$xdate1 = "01/31/2014";
And the second, to get "01/31/2015", use the date function. strtotime returns a timestamp, not a date format. Therefore, use
$xpire = date("m/d/Y", strtotime(date("m/d/Y", strtotime($xdate1)) . " +1 year"));
May I introduce a simple API extension for DateTime with PHP 5.3+:
$xdate1 = Carbon::createFromFormat('m/d/Y', '01/31/2014');
$xpire = $xdate1->addYear(1);
First make $xdate1 as string value like
$xdate1 = '01/31/2014';
then apply date function at it like bellow
$xpire = date('m/d/Y', strtotime($xdate1.' +1 year')); // 01/31/2015
What is a php function that i can make call tomorrows date formatted like this? 02/04/2014
So if im looking at the website on 2/3/2014 it will show 02/04/2014
If i look at it on 2/15/2014 it will show 2/16/2014
Just add using strtotime() / date() functions:
echo date('m/d/Y', strtotime('+1 day'));
Update
You can also do it using PHP's DateTime and DateInterval classes:
$date = new DateTime();
$date->add('P1D');
echo $date->format('m/d/Y');
This should work ..
<?php
function tomorrow()
{$date = date('m/d/Y');
sleep(24*60*60);
return $date;}
echo tomorrow();
?>
I am using this function to change date format.
echo date("mdy", strtotime('2013-11-04'));
It's return: 110413
Now i want to decode it.
How to get date like this format: 2013-11-04
Remember 110413 should be as a input of that function
Thanks
Try like this
<?php
$MyDate="110413";
$date = DateTime::createFromFormat('mdy',$MyDate);
echo $newformat=$date->format('Y-m-d');
?>
$mydate=date("Y-m-d", strtotime('2013-11-04'));
echo date("Y-m-d", strtotime($mydate));
try like this,
$date = date("mdy", strtotime("2013-11-04"));
$myDateTime = DateTime::createFromFormat('mdy', $date);
$newDateString = $myDateTime->format('Y-m-d');
echo $newDateString;
I have a date which is formatted simply as 19830210 (YYYYDDMM).
How would I reorder it to 02101983 (DDMMYYYY) using php?
I simply want to rearrange the digits in the date.
use createFromFormat(), do:
$date = "19830210";
$newFormat = DateTime::createFromFormat('Ydm', $date);
echo $newFormat->format('dmY'); //will give you 02101983
echo $output=date('dmY',strtotime('02101983'));
For future use with date format use below link for reference
http://php.net/manual/en/function.date.php
use substr() function:
<?php
$date1 = 'YYYYDDMM';
$date2 = substr($date1 , 4 , 4).substr($date1 , 0 , 4);
echo $date2;
?>
$str='19830210'; //(YYYYDDMM).
echo $str."<br/>";
$yy=substr($str,0,4);
$mm=substr($str,4,2);
$dd=substr($str,6,2);
$reorder=$dd.$mm.$yy;
echo $reorder; //(DDMMYYYY).
I have a date variable like 10-25-1998 and I only want to echo 10. Can anyone help me about it? Thanks...
$dateString=date("m-d-Y", mktime(0,0,0,10,25,1998));
echo date('m', $dateString); // not working here....will print out 01
strtotime will not recognize "m-d-Y" format, so the only way to get the month out of that string is to just parse it yourself, knowing that it's the first value in your string:
$dateString=date("m-d-Y", mktime(0,0,0,10,25,1998));
$t = explode('-',$dateString);
echo $t[0]; //month
echo $t[1]; //day
echo $t[2]; //year
$tomorrow = mktime(0,0,0,01,25,1998);
echo "Month is ".date("m", $tomorrow);
For more info look here: PHP Date() Function
$first2chars = substr($tomorrow, 0, 2);
picks the first 2 characters of the string $tomorrow
take care
You may want the getdate function:
$today = getdate();
echo $today["mon"];