PHP date_format(): How to format date from string value - php

I have a bit of PHP code:
$exd = date_create('01 Dec, 2015');
$exd = date_format($exd, 'Y-m-d');
echo $exd;
Which is used for formatting the date. The expected output would be 2015-12-01 but it returns 2016-12-01. What am i missing?

Use createFromFormat method first, provide the input format:
$exd = DateTime::createFromFormat('d M, Y', '01 Dec, 2015');
// arguments (<format of the input>, <the input itself>)
$exd = date_format($exd, 'Y-m-d'); // then choose whatever format you like
echo $exd;

The date_create() function accepts only the parameter link, This function is also and alias function of DateTime::__construct()
check the function date_create_from_format() its also a alias function of DateTime::createFromFormat(). Refer link
$exd = date_create_from_format('j M, Y', '01 Dec, 2015');
//$exd = date_create('01 Dec, 2015');
$exd = date_format($exd, 'Y-m-d');
echo $exd;

It can be a date function call simply. Use stringtotime for exact/precise date/time value
date("Y-m-d",strtotime("01 Dec 2015"))
When you run this code the out put will show
2015-12-01
this is because of the comma in the string which terminates the date string in the compiler. If you specify exactly the timezone (like
$timezone = 'America/New_York) . parameter you can show precise time as well.

i got the solution of your bug
that is date_format(datae_variable,date_format);
<?php
$exd = date_create('01 Dec, 2015');
$exd1 = date_format($exd,"Y-m-d");//here you make mistake
echo $exd1;
?>

Related

converting a string to date in php

I have a string that contains date and time. Format of my string is yyyymmddtime.
For example. 20171125123000209. this is my complete string in which first comes the month, then month and then day after that time. How can i retrieve date from it by converting to readable date format. I tried with php's date function. But the output was not as expected. Please help.
Try this
<?php
$str_date= "20171125123000209";
$exiting_date_format='Ymd';
//first 8 characters from given date string in second parameter below
$date = DateTime::createFromFormat($exiting_date_format, substr($str_date,0,8));
echo $date->format('Y-m-d');//specify desired date format
?>
Output :
2017-11-25
DateTime::createFromFormat - Parses a time string according to a specified format
You can use date() function of php
<?php
$full_date= "20171125123000209";
echo date('dS F h:i:s A', $full_date);
//Output = 31st May 12:30:09 AM
?>
Or,
To get date from timestamp like now,
$timestamp= time(); //Or your timestamp here
$date = date('d-m-Y', $timestamp); //Inside first parameter, give your date format
echo $date; //17-12-2017
Or,
To get anything from string you can also use substr() function of php.
<?php
$full_date= "20171125123000209";
$year = substr($full_date, 0, 4);
$month = substr($full_date, 4, 2);
$date = substr($full_date, 6, 2);
echo 'Year = '.$year.' ';
echo 'Month = '.$month.' ';
echo 'Date = '. $date.' ';
?>
Output:
Year = 2017 Month = 11 Date = 25
Test in jdoodle
About substr() function in php
It's not necessary to manipulate the string at all. PHP's DateTime class supports parsing a string containing miliseconds natively, using the u format modifier:
$str = '20171125123000209';
$date = DateTime::createFromFormat('YmdHisu', $str);
Using your new $date object, you can convert to whatever format you're looking for, e.g.
echo $date->format("F j Y, g:i a");
// November 25 2017, 12:30 pm
See https://eval.in/920636
$your_strtotime_val = ""; //20171125123000209
$convert_to_date = date("m-d-Y h:i:s",$your_strtotime); // [m-d-Y h:i:s] this depending on how you convert date in first time so be careful

Having trouble to convert a string date to a date object

This is the current date I got in PHP from getLastLogin():
Apr 22 2016, 01:44:17 CEST
This is the code that I use to convert it:
$ymd = null;
$dateObject = DateTime::createFromFormat('M d Y, H:i:s T', $player->getLastLogin());
if($dateObject){ $ymd = $dateObject->format("m/d/Y"); }
echo $ymd;
There is no error but it never goes into the if statement and therefore it's still null when I try to echo it.
My function getLastLogin() is working as well, so I think it's narrowed down to the actual format.
Thanks for help!

String Replace Date & Time

a line of my XML looks like this:
<observation_time_rfc822>Thu, 09 Oct 2014 22:59:16 +0200</observation_time_rfc822>
I grab it and give it out:
$ob_time= $xml->observation_time_rfc822;
echo $ob_time;
The output looks like this:
Thu, 09 Oct 2014 22:59:16 +0200
But what I need should look like this (yes, the funny '%3A' replaces ':')
2014-10-09+22%3A59%3A16
I think string replace can do this, please someone can help me to find out!
Thank you!
Edit: Use #Ghost's solution, it correctly handles the timezone offset.
First you need to reformat your date. You do this by parsing it with strtotime and formatting it with the date function. Those "funny %3A replaces" are actually URL-encoded characters:
$date = date('Y-m-d H:i:s', strtotime($ob_time));
$date = urlencode($date); // 2014-10-09+20%3A59%3A16
You could use DateTime class in this case, then use urlencode():
Example:
$ob_time = (string) $xml->observation_time_rfc822;
$date = DateTime::createFromFormat('D, d M Y H:i:s O', $ob_time);
$real_date = $date->format('Y-m-d H:i:s');
echo urlencode($real_date); // 2014-10-09+22%3A59%3A16

how to convert timestamp to date in codeigniter

I want to convert 1373892900000 to Monday 2013/07/15 8:55 AM in Codeigniter.
However, I keep receiving a totally different result by converting the timestamp using the function i have written, please note:I need to change the dates according to different timezones, that is why I want to write it this way:
public function time_convert($timestamp){
$this->load->helper('date');
date_default_timezone_set('UTC');
$daylight_saving = TRUE;
$timezone = "UM4"; //toronto or new york timezone
$time = gmt_to_local($timestamp, $timezone, $daylight_saving);
$final_time = standard_date('DATE_RFC822', $time);
return $final_time;
}
Result from the above function is: Sat, 08 Dec 06 01:40:00 +0000
And if I don't put date_default_timezone_set('UTC'); in the above function, I get this date instead Sat, 08 Dec 06 02:40:00 +0100. My codeigniter seems to default the timezone to Europe/Berlin.
Can anyone please help me correct any of the mistakes I might have made?
Why not just use PHP's date function?
public function time_convert($timestamp){
return date('l Y/m/d H:i', $timestamp);
}
For different timezones use a DateTime object:
public function time_convert($timestamp, $timezone = 'UTC'){
$datetime = new DateTime($timestamp, new DateTimeZone($timezone));
return $datetime->format('l Y/m/d H:i');
}
Think that should work. Note: I tihnk you need at least PHP version 5.20 for the TimeZone class.
<?php
$time_str=1373892900000;
echo gmdate("fill with your format", $time_str);
?>
your format = format your time in php, reading this page for details.
http://php.net/manual/en/function.date.php
http://php.net/manual/en/function.gmdate.php
Appears as though an invocation of standard_date with the DATE_ATOM format may sort you:
echo unix_to_human(time(), true, 'us'); # returns 2013-07-12 08:01:02 AM, for example
There are a whole host of other options for the format, enumerated on the linked page.
This how to covert timestamp to date very simple:
echo date('m/d/Y', 1299446702);
to convert timestamp to human readable format try this:
function unix_timestamp_to_human ($timestamp = "", $format = 'D d M Y - H:i:s')
{
if (empty($timestamp) || ! is_numeric($timestamp)) $timestamp = time();
return ($timestamp) ? date($format, $timestamp) : date($format, $timestamp);
}
$unix_time = "1251208071";
echo unix_timestamp_to_human($unix_time); //Return: Tue 25 Aug 2009 - 14:47:51
if you want to convert it to a format like this: 2008-07-17T09:24:17Z than use this method
<?php
$timestamp=1333699439;
echo gmdate("Y-m-d\TH:i:s\Z", $timestamp);
?>
for details about date:
http://php.net/manual/en/function.date.php
Your timestamp is coming from javascript on the client, I would guess, because it appears to be in milliseconds. php timestamps are in seconds. So to get the answer you want, first divide by 1000.
Showing the full year would have made the issue more obvious, as you would have seen the year as 45,506.

Passing a UK date in a URL

I am passing a date in a URL in a UK format as per the following:
http://www.website.com/index.php?from_date=01/04/2013&to_date=12/04/2013
The date range is 1st April 2013 to 12th April 2013.
Then I am using the following PHP to convert it to the YYYY-MM-DD format.
<?php
$rpt_from_date = date("Y-m-d", strtotime($_GET["from_date"]) );
$rpt_to_date = date("Y-m-d", strtotime($_GET["to_date"]) );
echo $rpt_from_date;
echo "</br>";
echo $rpt_to_date;
?>
For some reason this is not working. The above returns the following:
2013-01-04
2013-12-04
It's switching the month and day around. I want it to return:
2013-04-01
2013-04-12
Does anyone have any ideas?
Use DateTime object, to get php understand in which format you passing date to it.
$rpt_from_date = DateTime::createFromFormat('d/m/Y', $_GET["from_date"]);
echo $rpt_from_date->format('Y-m-d');
PHP is reading your time string in the US format (MM/DD/YYYY) because you are using slashes. You could use dashes to give the time: index.php?from_date=01-04-2013&to_date=12-04-2013, or convert it yourself:
$uktime = implode("-", explode("/", $_GET['from_date']));
I will provide the solution but it is not using the date function:
$arr = explode("/",$_GET["from_date"]);
$from_date = $arr[2]."-".$arr[1]."-"$arr[0];
Second solution is as following:
$from_date = implode(array_reverse(explode("/",$_GET["from_date"])));

Categories