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()
Related
Iam having variable with value 9:30 am - 12:00 pm i.e
$val=$shift (o/p 9:30 am - 12:00pm)
I want to convert it to 09:30 am - 12:00pm, i mean i need to add 0 infront of number if time having one digit
If you are using date function then you can format time using h.
For example
echo date('h:ia'); // output 09:50am
See Referance
If you have values from DataBase as you mentioned in comment then you need to apply PHP's built-in function strtotime() before formatting the time.
For Example
echo date( 'h:ia', strtotime('9:30 am') ); // output 09:50am
Assuming you are saving your time in your database as a string as 9:30 am - 12:00pm you can just split them. Then format each data.
$val= "9:30 am - 12:00pm";
$splittedString = explode('-', $val);
$time1 = date('h:ia',strtotime($splittedString[0]));
$time2 = date('h:ia',strtotime($splittedString[1]));
echo $time1." - ".$time2;
Try
echo date('h:ia',strtotime("9:30 am"))." - ".date('h:ia',strtotime("12:00pm"));
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');
When using the following
echo date('D',strtotime("2013-06-16T06:00:00-07:00"));
echo date('D',strtotime("2013-06-16T18:00:00-07:00"));
First it returns Sun and the Second returns Mon. I'm not really sure why or how to correct! The Date:"2013-06-16T06:00:00-07:00" is data I'm retrieving from a XML file. The timestamp has the correction for UTC at the end not sure if this is generating the error.
Thanks for any help.
To get expected results you should consider using DateTime():
<?php
echo date('D',strtotime("2013-06-16T06:00:00-07:00")) . "\n";
echo date('D',strtotime("2013-06-16T18:00:00-07:00")) . "\n";;
$dt1 = new DateTime("2013-06-16T06:00:00-07:00");
$dt2 = new DateTime("2013-06-16T18:00:00-07:00");
echo $dt1->format('D') . "\n";
echo $dt2->format('D') . "\n";
Output
Sun
Mon
Sun
Sun
Fiddle
This is because The Date represents the time is in time zone specified in date.timezone settings. So the timezone -07:00 is parsed and converted back to date.timezone timezone.
To understand the idea just add e in the date string
echo date('D e',strtotime("2013-06-16T06:00:00-07:00"));
echo date('D e',strtotime("2013-06-16T18:00:00-07:00"));
See example.
Its better you use DateTime(). It does not have such limitation.
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"])));
I have the following date format in a xml sheet:
Jan 30
and I want to display as:
2011-01-30
2011 being the current year
Can someone help me with that?
strtotime will use the current year if none is specified, so this would work
$t=strtotime("Jan 30");
echo strftime("%Y-%m-%d", $t);
strtotime + date gives you:
echo date("Y-m-d", strtotime('30 Jan')); //echoes '2011-01-30'
care to post the xml?
in anycase, take the string
$x = "Jan 30"
add 2011
$x = $x . ' ' . date('Y');
convert it to date time and format it
$y = date_format('Y-m-d', strtotime($x));
echo $y;`
strtotime is your key here, it converts most any date format into seconds since the epoch, then all you need do is use the date_format function
ps... if you like my answer, accept it as the answer to bring your accepted question ratio, otherwise people will be less likely to answer your questions