I have the following date:
2014-09-26 11:30:00
I need to display it in the page like:
26-September-2014 At 11:00
I have the following code to do this:
$exp_datetime = date('d-F-Y At H:s', strtotime($exp_datetime)); // So stupid I know ;)
echo "Visit Date: " . $exp_datetime;
and I get the following result:
Visit Date: 26-September-2014 AM30 11:00
I need to know how can i get the required result!
Many many thanks in advance! :)
You need to escape your "At" word. Because, as described here: http://php.net/manual/fr/datetime.format.php "A" and "t" are reserved characters.
So your correct code should be:
$exp_datetime = date('d-F-Y \A\t H:s', strtotime($exp_datetime));
$exp_datetime = date('d-F-Y At H:s', strtotime($exp_datetime));
^
At is not an accepted date format character. You can do like
echo "Visit Date: ". date('d-F-Y', strtotime($exp_datetime));
echo " At ". date('H:s', strtotime($exp_datetime));
Alternatively, you could also use strftime() in this case:
echo strftime('%d-%B-%G At %H:%M', strtotime('2014-09-26 11:30:00'));
// 26-September-2014 At 11:30
Also using datetime
$d = new DateTime('2014-09-26 11:30:00');
echo $d->format('d-F-Y \A\t H:s');
Related
I am getting ISO datetime using below code
$date= date("c");
echo $date;
which returns something like below
2016-01-07T20:18:46+00:00
But the API I use tells that its wrong format and it needs in below format:
2016-01-07T20:35:06+00Z
I need to remove :00 at the end and add Z.
I am completely new to regex , can anyone help me understand the regex and tell which format is required.
You'll want to define the date format specifically.
If microseconds will always be 00
date("Y-m-d H:i:s+00\Z");
Else, use this little big of logic
date("Y-m-d H:i:s+"). substr((string)microtime(), 2, 2) . 'Z';
More info.
http://php.net/manual/en/function.date.php
You can find the last occurrence of : with strrpos, and get the substring up to it with substr, and then add Z:
$date= date("c");
echo "Current: $date\n"; // => 2016-01-07T21:58:08+00:00
$new_date = substr($date, 0, strrpos($date, ":")) . "Z";
echo "New : " . $new_date; // => 2016-01-07T22:10:54+00Z
See demo
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
Suppose the current year is 2014; then I want get the date 2014-06-30, if the current year is 2015 then I want 2015-06-30
I tried date('Y'); but it is just giving current year.
You can just use:
date("Y-06-30")
Demo : https://eval.in/103028
try:
echo date("Y-06-30");
OUTPUT:
2014-06-30
See date documentation:
http://php.net/manual/en/function.date.php
You can concatenate the rest of your required date to your date() function as follow
echo date('Y') . '-06-30';
//output 2014-06-30
Use this :
date("Y-06-30");
Y will be the your year and rest will same.
Where y is A full numeric representation of a year, 4 digits for more Info : Date()
You need to put like below code:
$d = date('Y');
$data = $d."-06-30";
echo $data;
I am tryig to check timestamp using php. However, the $date variable echoes out a timestamp
1382389873 which I checked with a unix converter epochconverter.com it shows Mon, 21 Oct 2013 21:11:13 GMT when it is meant to be 5 Nov 2013. Anyone can see the code below and pinpount my mistake? Thanks.
$today = strtotime(date("d.m.y"));
$c_date = strtotime($CI->session->userdata('c_date'));
$early = strtotime(date("d.m.y")."+2 week");
$date = strtotime("5.11.13");
echo'<br/>';
echo 'test'.$date;
echo'<br/>';
echo 'collection'.$c_date;
echo'<br/>';
echo 'early'.$early;
echo'<br/>';
echo 'today '.$today;
echo'<br/>';
strtotime is very vague in the way it processes dates. It is interpreting "5.11.13" as 5:11:13pm today (Which is 21:11:13 on a 24-hour clock).
If you want to specify november 5th you should do it like so:
$date = strtotime("11/5/13");
echo $date;
echo date("m/d/Y", $date);
Output:
1383609600
11/05/2013
Others answered this before I did, but here are my tests and notes to supplement the conversation... strtotime() converts a string to a time integer. date() converts an integer to a (optionally formatted) string. I was gonna say you can always just use that rather than epochvonverter.com... but now I understand why you went there. Also note that the manual says this:
"Note: Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed.
To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible." -http://www.php.net/manual/en/function.strtotime.php (more quirky things on that page, btw).
It is very interesting that this example with dot is NOT producing d.m.y, but h.m.s! I guess that's because we're working with strtoTIME, not... uh.. strtoDATE :)
Consider this:
$integer = 1382389873;
var_dump($integer);
echo "<br>";
//int(1382389873)
$string = date($integer);
var_dump($string);
echo "<br>";
//string(10) "1382389873"
$formatted=date('d.m.y',$integer);
$laterint=strtotime($formatted."+2 week");
var_dump($laterint);
echo "<br>";
// int(1383682213)
$laterstring=date('d.m.y',$laterint);
var_dump($laterstring);
echo "<br>";
// string(8) "05.11.13"
And here's the juicy part:
$date1 = strtotime("5.11.13");
var_dump($date1);
echo "<br>";
$datestring1=date('d.m.y',$date1);
var_dump($datestring1);
//BAD PHP, BAD!
echo "<br>";
$date2 = strtotime("5.11.2013");
var_dump($date2);
echo "<br>";
$datestring2=date('d.m.y',$date2);
var_dump($datestring2);
echo "<br>";
$date3 = strtotime("5/11/13");
var_dump($date3);
echo "<br>";
$datestring3=date('d.m.y',$date3);
var_dump($datestring3);
echo "<br>";
Really interesting stuff, guys - thank you all! The moral of the story for me is to always be explicit with 4 digit year.
I ran a couple of tests for you and it seems that you need to put your year in YYYY format e.g. 2013:
$date = strtotime("5.11.2013"); // your code
echo $date . "\n";
echo date('d-m-Y', $date);
// timestamp: 1383562800
// converted to date: 05-11-2013
--edit-- as I mentioned in my previous comment, if $date is supposed to be the same as $early, why are you even bothering to reassign it manually? Why not just use $early?
How do I get the system date and time in this format:
$systime="12/january/2010 10.30 AM "
To get exactly what you've asked for, you'll need to use strtolower() and date:
$systime = strtolower(date("d/F/Y G.i")) . " " . date("A") . " ";
You need strtolower because there's no built-in way to get lowercase month values, so you need to get that part as January and then transform it to lowercase. You can't lowercase the whole thing because you seem to want AM or PM rather than am or pm.
Using date and strftime() we can get the system date.
Example:
echo date("d/F/Y g:i A");//It prints 05/March/2010 12:18 PM
echo strftime("%d/%B/%Y %I:%M %p"); //It prints 05/March/2010 12:20 PM
Try:
$systime = date('d/F/o g i A');
Sample output:
05/March/2010 7 27 AM
date() and time()