So this should be a real easy question but I can't seem to find a simple answer anywhere.
I'm patching up some PHP code (I'm not a PHP'er) and I have this variable $orderDate. How do I print this variable so that its just M/d/yy h:mm tt?
Update:
So I looked around and saw what $orderDate is. Here's the code:
global $orderDate;
$orderDate = strftime('%c');
print("Order Date: ".date("M/d/Y h:M", $orderdate)."<br />");
so I get this for output:
Dec/31/1969 06:Dec
and should be getting today's date....
echo date("m/d/Y h:m", $orderDate);
echo date("m/d/Y h:m", strtotime($orderDate)); // or this
Depends on what $orderDate contains.
Look into date() since it has there plenty of examples and is pretty simple to use.
UPDATE:
$orderDate = date("M/d/Y h:M");
print("Order Date: ".orderDate ."<br />");
Also check out to see if this works for you.
date function will do that for you.
If $orderDate is an integer time stamp, you probably want strftime. Specifically, I think the call you want would be:
strftime("%D %l:%M %p", $orderDate)
However, I recommend reviewing the web page to make sure I've interpreted what you want correctly.
See the PHP date() function.
Returns a string formatted according to the given format string using the given integer timestamp or the current time if no timestamp is given.
string date ( string $format [, int $timestamp = time() ] )
Related
This might be very simple, but I still need help with this.
I have this portion of php code:
$END = NULL;
if (isset ($_POST['end'])){
$END = date_format($_POST['end'], "Y-m-d H:i");
}
Where $_POST['end'] is a date and time that I get in format dd-mm-YYYY HH:mm. The problem is, as you can guess, that it doesn't transform my input to the Y-m-d H:i format, it just doesn't do anything. But I've followed what I've seen in another code that does indeed work. What am I doing wrong here?
Ignore the fact that I don't check if the input is well written, I assume that it will be.
This is because date_format accepts an object and not a string. You should use the function date and pass to it's second argument a timestamp.
Use date_create() function to convert string to date and then pass it to date_format() function.
if (isset ($_POST['end'])){
$date = create_date( $_POST['end'] );
$END = date_format($date, "Y-m-d H:i");
}
I currently get duration in this format: 04:00 but I need in this format 00:04:00.
I am using this code but it's not working correctly.
$time = date("h:i:s", strtotime($duration));
echo $time;
try like this,
$duration = "00:"."04:00";
This should work for you:
(Your problem is in which format you read in your time, so DateTime::createFromFormat)
$date = DateTime::createFromFormat("i:s", "04:00");
echo $date->format('H:i:s');
Output:
00:04:00
The problem is the strtotime($duration); It doesn't know exactly what format that's in, and can't get a valid date from it. (This is why you're getting unexpected results from the date function.
If you want to just simply add 00: before $duration, you can do it like so:
$duration = '04:00';
$duration = '00:' . $duration;
You then would be able to pass this to the strtotime function, and would likely get better results.
So I have a field in my database called 'DateTime' and the following lines of code:
echo "Date/Time: ";
echo $row['DateTime'];
How do I format it so that instead of being like this:'2013-02-07 22:14:56', it will be like this: '07/02/13 - 22:14'
Thanks.
Alternatively you could use:
DateTime::createFromFormat('Y/m/d H:i:s',$row['DateTime']); this will give you a datetime object, which are quite nice to work with.
Another alternative would be to have MySQL format the DATETIME value as a string in the desired format, using the DATE_FORMAT function.
SELECT DATE_FORMAT(`DateTime`,'%d/%m/%y - %H:%i') AS `DateTime`
...
No change required to your PHP code except for the SQL text sent to the database server.
This approach can very efficient, and reduce the amount of code you need, if all you are doing with this string is displaying it. If you are doing any sort of manipulation on this value, then casting the string value returned from MySQL resultset into a datetime object is probably a better way to go.
A demonstration of the DATE_FORMAT function:
SELECT DATE_FORMAT('2013-02-07 22:14:56','%d/%m/%y - %H:%i') AS `DateTime`
DateTime
----------------
07/02/13 - 22:14
how to output date into Year textbox Month textbox Day textbox
$book_date = $myrow["Publication_Day"];
$book_year = Date("Y", strtotime($book_date));
$timestamp contains ur date & time in any format.....................
date('Y/m/d - H:i',strtotime($timeStamp));
echo date('d/m/y H:i', strtotime($row['DateTime']));
See date and strtotime for more detail on the functions from the docs
$mytime = strtotime('2013-06-07 22:14:56');
$newDate = date('m/d/y - G:i', $mytime);
echo $newDate;
Here's an alternative using DateTime. If you're working with timezones this code can be easily modified to handle that.
$datetime = new DateTime('2013-02-07 22:14:56');
echo $datetime->format('d/m/y H:i');
See it in action
I am using this, but it's not working for date 2012,02,26:
$theDate = "2012,02,26";
$timeStamp = StrToTime($theDate);
$in6days = StrToTime('+6 days', $timeStamp);
$newdate = date("{$theDate}", strtotime('+1 day', strtotime($in6days)));
echo "$newdate";
showing 2012,02,32
I don't think that 2012,02,26 is a valid format that strtotime() will accept. Valid date formats are listed here: PHP: Date Formats
In order to check if the strToTime function works or not , try:
echo $timeStamp;
If you get false then you should use another data format as christophmccann recommended,
for instance:
$theData = "02/16/2012"; //or the next one
$theData = "30-6-2008";
You should be working internally with widely used date formats - either unix timestamp or RFC 2822 if you have good reason to. Use date() to reformat your date according to your own display requirements if you wish (see php.net/date).
So, you can show today in your preferred date format using echo date('Y,m,d');
I have a date in this format:
24-12-2010 // DAY - MONTH - YEAR
I need to get it in this format:
1995-12-31T23:59:59.999Z // The Z is for the TimeZone I think.
Check this link out:
http://lucene.apache.org/solr/api/org/apache/solr/schema/DateField.html
The above link is the way I need the date.
I am using PHP now, so this needs to be with PHP.
How can I convert these dates the easiest way?
Thanks
That is an ISO8601 format date; the following is what you want.
gmdate('Y-m-d\TH:i:s\Z', strtotime($date_value));
You can do something like that:
$dateTime = new DateTime($myDate);
$formatted = $dateTime->format("Y-m-d\TH:i:s.z\Z");
The mentioned solution with:
$dateTime->format(DateTime::W3C);
$dateTime->format(DateTime::ISO8601);
does return strings like:
2012-11-28T17:21:11+0100
which cannot be parsed, at least with newer Solr versions.
I wouldn't use gmdate if you need to support timezones. The DateTime implementation is well done, and is also available for functional programming.
http://php.net/manual/en/class.datetime.php
http://php.net/manual/en/ref.datetime.php
You can use the DateTime class
$dateTime = new DateTime();
$dateTime.setDate(24, 12, 2010);
$output = $dateTime.format(DateTime::W3C);
// Output now is your date in W3C format.
use the date ( string $format [, int $timestamp ] ) function of php!
In second paramter use http://php.net/manual/en/function.strtotime.php to get the timestamp from strings
$date = strtotime('24-12-2010');
$new_date = gmDate("Y-m-d\TH:i:s.z\Z",$date);