How do get this date/time format? - php

I saw a date/time format in a PHP code that has something like this:
2015-06-19T00:38:04Z
How can I generate a datetime having that kind of format?
I know this kind of format date("Y-m-d H:i:s"); outputs different 2015-06-19T00:38:04Z because it has a T and a Z.
So it's like I want to get the current date/time having a T and a Z

Escape T and Z in format
echo date("Y-m-d\TH:i:s\Z"); // 2015-06-17T16:50:50Z
UPDATE Thanks to Lithis. To take a GMT/UTC time
echo gmdate("Y-m-d\TH:i:s\Z"); // 2015-06-17T17:06:06Z

Related

separating a weird formatted timestamp into an understandable date

I'm working with an XML document that is returning variables and for some reason in a xml return the timestamp is formatted like this... 20180606T110000 ... why anyone would format it like that makes no sense to me; however, its what I have to work with. ITs formatted YYYYMMDD , the T is the split between date and time, HHMMSS. ITs set up in a 24 Hour clock that I also need to convert to 12 hr clock with am/pm
I need that formatted like 06/06/2018 11:00:00 AM.
Is there a way to do that via a date format (I know how to use date() but I don't know how to bring in that timestamp the way its formatted) or even separating it out into
$year = xxxx
$month = xx
$day = $xx
$Hour=xx
etc. etc. etc.
if need be.
I've briefly looked at php's date create from format ( date_create_from_format('j-M-Y', '15-Feb-2009') ) but dont fully understand how that works.
I've also thought about a split. I've also looked at chunk_split and wordwrap but its not even amounts of characters so that would be complex to create.
Any ideas?
The format you're working with is "XMLRPC (Compact)" format. This is fully supported by PHP (you can see a list of supported formats here). To get what you want, just use a combination of strtotime() and date().
$timestring = "20180606T110000";
$timestamp = strtotime($timestring);
echo date("m/d/Y h:i:s A", $timestamp);
You can use PHP DateTime to parse a datetime String with any format. Please view the Parameters format in the following link to understand how the "Ymd\THis" part works: http://php.net/manual/en/datetime.createfromformat.php
<?php
$time = "20180606T110000";
$date = DateTime::createFromFormat("Ymd\THis", $time);
// 06/06/2018 11:00:00 AM.
echo $date->format("d/m/Y h:i:s A");

Convert timestamp into 2016-12-24T14:11:00Z

Hi I'm trying to convert my result out of a timestamp field into a formated way.
So from '2016-12-24 14:11:00' to '2016-12-24T14:11:00Z'
What is the easiest way to do so?
It looks like ISO 8601 format which can be achived by date with c param.
echo date('c', strtotime('2016-12-24 14:11:00'));
but if u want exactly that format you could personalize it
echo date('Y-m-d\TH:i:s\Z', strtotime('2016-12-24 14:11:00'));
If z letter means something then probably you need to find proper parameter on http://php.net/manual/en/function.date.php
SELECT concat(replace(`date_field`,' ','T'),'Z')

want to convert my date formate 21/10/60 to 21-10-1960

I want to change date format from "21/10/60" to 21-10-1960 in PHP
i am using this code:-
$date = DateTime::createFromFormat('d/m/y', '21/10/60');
echo $date->format('Y-m-d');
and I am getting output:-
2060-10-21
but I want in this:-
1960-10-21
How can i Do this.
Since you mentioned repeatedly in your comments, like this one: minimum age is 16 years old, it will always be 1900's. You simply need
list($d,$m,$y)=explode("/","21/10/60");
echo "$m-$d-19$y"; // m-d-Y
echo "19$y-$m-$d"; // Y-m-d
No date functions or complicated stuff is required. Its a simple mapping job. As long as your actual date is valid and conforms to the format you mentioned, this output will be valid as well.
Fiddle

How to insert time with am/pm format into the database

So im trying to insert a time using an input text field into a database table with data type TIME.
The format of time that I want to insert should be like this:
H:MM pm// example: 6:30 pm
The problem is the am/pm doesnt insert in my database table.
Only the hour and minute.
Please give me idea how to solve this.
Better with sample codes. Thanks.
Data Type TIME is for storing time data type - that means no AM/PM. Store the data in Your database in 24 hour format and format it to 12 hour format with am/pm in PHP or MySQL using one of these:
PHP:
$date = new DateTime($mysql_column['time']);
$date->format('h:i:s a');
or:
$date = date('h:i:s a', strtotime($mysql_column['time']));
or MySQL:
SELECT DATE_FORMAT('%h:%i:%s %p', time) FROM table;
Store the TIME as a standard format (18:30:00), and the format it however you want when you display it (Using DateTime objects or the date functions).
MySQL doesn't support extra formats when storing time data.
I think you want to add the jquery time picker value in your database with actual format in the database.
Here I have written some function
function update_time($time){
$ap = $time[5].$time[6];
$ttt = explode(":", $time);
$th = $ttt['0'];
$tm = $ttt['1'];
if($ap=='pm' || $ap=='PM'){
$th+=12;
if($th==24){
$th = 12;
}
}
if($ap=='am' || $ap=='AM'){
if($th==12){
$th = '00';
}
}
$newtime = $th.":".$tm[0].$tm[1];
return $newtime;
}
$time = update_time($_POST['time']); //here I am calling the function now you can insert the value in db
you just have to call the function and insert the returned value in database.
And while printing that you can do something like that echo date("h:i A",strtotime($time));
Change the type of the field to a varchar. TIME cannot store it like that. However, keep in mind that storing it like you want to will make it more difficult to provide localized results if that is something you will eventually need. That is, timezone support becomes difficult if you are not storing the timestamp itself, but rather a user-friendly representation.
EDIT: Or, DATETIME works as well, as was pointed out in the comments above.
You can use the DateTime Object in PHP which has functions to create a time object from any format and also has a function to output a time in any format like so
<?php
$date = DateTime::createFromFormat('j-M-Y', '15-Feb-2009');
echo $date->format('Y-m-d');
?>
http://php.net/manual/en/datetime.createfromformat.php
You would be best changing the field type to 'VARCHAR (32)', and then writing the time with PHP.
Example: date('m/d/y g:i:sa');
Why do you want to store the am or pm anyhow? If you store the date/time as a unix epoch timestamp, you can format the date however you want in the program - not the database.
Example: time(); - Store this in an INT(8) field.
date('m/d/y g:i:sa, $time()); - Output from DB like this.
try .ToShortTimeString() after your date variable.

Convert SQL Time using PHP?

I want to convert MySQL Time data type using PHP and Javascript. I know it can be done using FORMAT_TIME of MySql, but I would like to do the same with php. The Time format is hh:mm:ss by default and I would like to convert it to hh:mm.
Just feed the date into the following function(s):
<?php echo date("h:i", strtotime($date)); ?>
//any date is ok, we care only about the hours and minutes
// $your_date is what comes from db in format hh:mm:ss
$date = new DateTime('2000-01-01 '.$your_date);
echo $date->format('H:i');
I think you could create a DateTime object and then use format("h:i") to get the desired output

Categories