How to decode Date or time string - php

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;

Related

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.

PHP date dd-mm-yyyy

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!

PHP date conversion from Facebook API

I have a problem by converting a date in proper format.
I get the time the from Facebook API in this format: 2013-08-23T09:00:00
I then $fbdate = date('2013-08-23T09:00:00');
When I echo $fbdate, it retuns 2013-08-25UTC05:00:00.
Then I tried:
$datum = date("d.m.Y",$fbdate);
$uhrzeit = date("H:i",$fbdate);
To extract the date and the time but it always returns:
01.01.1970 for $datum and 00:33 for $uhrzeit.
You should use strtotime() to parse a date string into a UNIX timestamp:
$fbdate = strtotime('2013-08-23T09:00:00');
$datum = date('d.m.Y', $fbdate);
$uhrzeit = date('H:i', $fbdate);
Try using the DateTime class:
$fbdate = '2013-08-23T09:00:00';
$date = DateTime::createFromFormat('Y-m-d\TH:i:s', $fbdate);
$datum = $date->format('d.m.Y');
$uhrzeit = $date->format('H:i');
echo $datum;
echo $uhrzeit;
$datum = date("d.m.Y",strtotime($fbdate));
$uhrzeit = date("H:i",strtotime($fbdate));
The date function in PHP is meant to convert a microtime into a date, so you need to convert your string dates to microtimes first.
Have you tried this? You should use strtotime() when using the date function.
$datum = date("d.m.Y", strtotime($fbdate));
$uhrzeit = date("H:i", strtotime($fbdate));
Use strtotime()
$a = date("Y-M-d", strtotime($datum));
echo $a.$uhrzeit;
More info http://php.net/manual/en/function.strtotime.php
I'm not a huge fan of using timestamp.
What I did to solve this issue was:
$updatedDate = new DateTime(
preg_replace('/^(.*)\+0000$/', '$1', $fbUser->getProperty("updated_time")),
new DateTimeZone("UTC")
);
It chops the +0000 part and creates the DateTime object with an explicit UTC timezone.

convert dd-mm-yyyy to yyyy-mm-dd in php

I tried the following code to change the dateformat from dmy to ymd, but when using i got wrong dates.
My code
$sdate11=date("Y-m-d", strtotime($_POST["txtstartdates"]) );
$sdate111=date("Y-m-d", strtotime($_POST["txtenddates"]) );
dates inserted were
30-05-2013 and 31-05-2013
the date it returned was
2035-11-03 and 2036-11-02
could you please help me to find what was the problem here and solve it
Thank you.
Try with split like
$a = split('-',$_POST["txtstartdates"]);
or you can use explode even like
$a = explode('-',$_POST["txtstartdates"]);
$my_new_date = $a[2].'-'.$a[1].'-'.$a[0];
Here strtotime will not work for the format dd-mm-yyyy
You might use DateTime for that:
$date = DateTime::createFromFormat('d-m-Y', $_POST['txtstartdates']);
echo $date->format('Y-m-d');
$date = DateTime::createFromFormat('d-m-Y', $_POST['txtenddates']);
echo $date->format('Y-m-d');
Can't you use the DateTime object to convert the date into the format you want?
$DateTime = new DateTime($_POST['FIELD']);
echo $DateTime->format('Y-m-d');
Your code seems correct , I dont know why its not working for you Can you try below
I have made the '' from "" only
$sdate11=date('Y-m-d', strtotime($_POST['txtstartdates']));
$sdate111=date('Y-m-d', strtotime($_POST['txtenddates']));
You can do it using single line as:
$show_date = DateTime::createFromFormat('d-m-Y', $dateInput)->format('Y-m-d');

Convert to date format dd/mm/yyyy

I have the following date: 2010-04-19 18:31:27. I would like to convert this date to the dd/mm/yyyy format.
You can use a regular expression or some manual string fiddling, but I think I prefer:
date("d/m/Y", strtotime($str));
<?php
$test1='2010-04-19 18:31:27';
echo date('d/m/Y',strtotime($test1));
?>
try this
If your date is in the format of a string use the explode function
array explode ( string $delimiter , string $string [, int $limit ] )
//In the case of your code
$length = strrpos($oldDate," ");
$newDate = explode( "-" , substr($oldDate,$length));
$output = $newDate[2]."/".$newDate[1]."/".$newDate[0];
Hope the above works now
There is also the DateTime object if you want to go that way: http://www.php.net/manual/en/datetime.construct.php
Simply Try
$date=date('d/m/Y',strtotime($yourdatevariable))
or else you want to convert to d-m-Y
Just Simply Edit above d/m/Y with d-m-Y
Try this:
$old_date = Date_create("2010-04-19 18:31:27");
$new_date = Date_format($old_date, "d/m/Y");
$source = 'your varible name';
$date = new DateTime($source);
$_REQUEST["date"] = $date->format('d-m-Y');
echo $_REQUEST["date"];

Categories