Php - How to reorder a date - php

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).

Related

get date from dateTime in PHP

I want to extract date without time from a single value from array result thaht I got.
I tried using array_slice but it didn't work.
My code..
$dateRows = $this->get('app')->getDates();
$dateRow = json_decode(json_encode($dateRows[0], true));
dump($dateRow[0]);die;
And I got result..
"2014-01-01 00:00:00"
And I want it to return just
"2014-01-01"
Very Simple, just use date_create() on your date & then format it using date_format() as follows -
$date = date_create("2014-01-01 00:00:00");
echo date_format($date,"Y-m-d");
So, in your case, it would be something like -
$date1 = date_create($dateRows[0]);
echo date_format($date1,"Y-m-d");
you can use strtotime function as well for getting formatted data
$a = "2014-01-01 00:00:00";
echo date('Y-m-d', strtotime($a));

Get Year-Month using simple one line code

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.

Add 1 year to a date

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

How to decode Date or time string

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;

PHP..How to print month only from a date variable

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"];

Categories