So I am using an API and I am getting the following updatedOn response: "UpdatedOn": "/Date(1674542180860+0000)/".
I am using the following code to convert it to m/d/y format:
if (!empty($data->Data->Shipment->UpdatedOn)):
$timestamp = preg_replace('/[^0-9+-]/', '', $data->Data->Shipment->UpdatedOn);
$date = new DateTime("#$timestamp");
$timestamp = $date->getTimestamp();
$parse_date = date('m/d/Y', $timestamp);
echo '<span>' . $parse_date . '</span>';
endif;
The issue is that it's not outputting properly, so I need some help - Here is the output that I am receiving:
02/17/55034
I would like to output it in the following format: January 12th, 2023.
Could anyone help me with my code?
In our decade timestamp is a 10 digit number
So this nember => "1674542180860" must be devide to 1000,
And if these two numbers "1674542180860" and "0000" must be added together, we should explod this string and sum two number,
I suggest you try this:
if (!empty($data->Data->Shipment->UpdatedOn)){
$timestamp = preg_replace('/[^0-9+-]/', '', $data->Data->Shipment->UpdatedOn);
$timestamp = explode('+',$timestamp);
$timestamp = (int)$timestamp[0] + (int)$timestamp[1];
$timestamp = (int)($timestamp/1000);
$parse_date = date('F d\t\h Y', $timestamp);
echo '<span>' . $parse_date . '</span>';
}
This is your favorite format to show date => "F d\t\h Y"
if(!empty($data->Data->Shipment->UpdatedOn)){
$parse_date = date('M d, Y', strtotime($data->Data->Shipment->UpdatedOn));
echo '<span>' . $parse_date . '</span>';
}
Related
i worked on the code to get subscription start date & end date from the infusionsoft and it return in this format
Start date=20151217T00:00:00
EndDate=20161217T00:00:00
how i can format to YYYY/MM/DD H:M:S ?
and what the 'T' stands for
The T, according to the the ISO 8601 standard, is the delimiter between the date and time parts.
See: ISO 8601 standard, Wikipedia
There are two functions you could use:
strtotime(date_time_string)
Converts a date/time string into a timestamp.
date(format, timestamp)
Formats a timestamp into a string according to "format".
The example code…
$so_string = '20151217T00:00:00';
$eo_string = '20161217T00:00:00';
$sd_stamp = strtotime($so_string);
$ed_stamp = strtotime($eo_string);
$f_datetime ='Y/m/d H:i:s';
echo 'Start date<br />';
echo ' original: ' . $so_string . '<br />';
echo ' timestamp: ' . $sd_stamp . '<br />';
echo ' formatted: ' . date($f_datetime,$sd_stamp) . '<br /><br />';
echo 'End date<br />';
echo ' original: ' . $eo_string . '<br />';
echo ' timestamp: ' . $ed_stamp. '<br />';
echo ' formatted: ' . date($f_datetime,$ed_stamp) . '<br />';
…will produce this output:
Start date
original: 20151217T00:00:00
timestamp: 1450306800
formatted: 2015/12/17 00:00:00
End date
original: 20161217T00:00:00
timestamp: 1481929200
formatted: 2016/12/17 00:00:00
Assuming that you have the current start and end dates stored in $startDate and $endDate variables...
$formattedStartDate = date("Y/m/d H:i:s", strtotime($startDate));
$formattedEndDate = date("Y/m/d H:i:s", strtotime($endDate));
This makes use of the date() and strtotime() functions
As far as the "T" goes...
Note that the "T" appears literally in the string, to indicate the
beginning of the time element, as specified in ISO 8601.
https://www.w3.org/TR/NOTE-datetime-970915
You can format the time using date function.
date('YYYY/MM/DD H:M:S',strtotime('$date_value'));
This uses DateTime with the createFromFormat function.
For more information, click here.
Requires PHP 5 >= 5.3.0, PHP 7
//infusionsoft format
$startDate = '20151217T00:00:00';
$endDate = '20161217T00:00:00';
//create DateTime object
$startDateTime = DateTime::createFromFormat('Ymd\TH:i:s', $startDate);
$endDateTime = DateTime::createFromFormat('Ymd\TH:i:s', $endDate);
//get DateTime with specified format
$newStartTime = $startDateTime->format('Y/m/d H:i:s');
$newEndTime = $endDateTime->format('Y/m/d H:i:s');
I have to do the following,
My date have two formats as Y-m-d H:i:s and Y-m-d
My requirement is,
if date contains Hours(H), need to convert to F j, Y, g:i a
else to F j, Y
Is there any method that I can check H in Y-m-d H:i:s
Thanks in advance
Y-m-d date format is 10 chars in length and Y-m-d H:i:s date format is 19 chars in length . Based on the length you can find if it has a date part . Even if it is 19 chars in length , the time part could be 0 in which case you would disregard the time part . Along this lines should work :
// $date = "2013-09-26";
// $date = "2013-09-26 00:00:00";
$date = "2013-09-26 00:00:01";
if( strlen( $date ) == 10 )
{
// Include code to convert the date to F j, Y
echo $date , ' Does not have time part .';
}
else
{
if( strlen( $date ) == 19 and substr( $date, 10, 8 ) <> '00:00:00' )
{
// Include code to convert the date to F j, Y, g:i a
echo $date , ' Has time part which is not 0 .';
}
else
{
// Include code to convert the date to F j, Y
echo $date , ' Has time part but is 0 .';
}
}
Try this...
if(strstr($date, ' ') !== FALSE) {
$date = date('F j, Y, g:i a', strtotime($date));
} else {
$date = date('F j, Y', strtotime($date));
}
something i use when getting a date from a db that may or may not come with the time. modify as needed.
IF(
TIME( {$val['field_name']} ) = '00:00:00',
DATE_FORMAT(DATE( {$val['field_name']} ),'%D %M %Y'),
DATE_FORMAT({$val['field_name']},'%l:%i%p, %D %M %Y')
)
Its a little verbose - as #Bamar said you can just check for spaces, but this will work even if there are leading or trailing spaces in the field:
$date_format = "\s*\d\d\d\d-\d\d-\d\d\s*";
$date_time_format = "\s*\d\d\d\d-\d\d-\d\d\s\d\d:\d\d:\d\d\s*";
if(preg_match($date_time_format, $subject)){
$subject = date('F j, Y, g:i a', strtotime($subject));
}
elseif(preg_match($date_format, $subject)){
$subject = date('F j, Y', strtotime($subject));
}
else{
//formatting error - you can decide how to format.
}
I'm receiving some data from an HTTP POST which includes what is labelled a GMT Timestamp:
<gmt_timestamp>201308031525</gmt_timestamp>
I then need to take this timestamp and convert it to this format:
MM/DD/YYYY HH:MM
So far I've been trying this:
$ts = $_GET['timestamp'];
$date = DateTime::createFromFormat('ymdHi', $ts);
$fmTimestamp = $date->format('m/d/Y h:i:s A');
but that generates a "PHP Fatal error: Call to a member function format() on a non-object" for the 2nd line. Any idea what I'm doing wrong?
You have a bug in this line:
$date = DateTime::createFromFormat('ymdHi', $ts);
You need an uppercase Y for the year:
$date = DateTime::createFromFormat('YmdHi', $ts);
A lowercase y indicates "A two digit representation of a year", whereas you need Y ("A full numeric representation of a year, 4 digits"). See the docs here.
You also need to set the timezone before you begin:
date_default_timezone_set('UTC');
(PHP does have a GMT timezone, but it shouldn't be used. UTC behaves the same as GMT within PHP.)
Edit
To get your desired output format of:
MM/DD/YYYY HH:MM
you need to do:
$fmTimestamp = $date->format('m/d/Y H:i');
Also, since you're "receiving some data from an HTTP POST", you need to use $_POST instead of $_GET:
$ts = $_POST['timestamp'];
So the complete code is:
date_default_timezone_set('UTC');
$ts = $_POST['timestamp'];
$date = DateTime::createFromFormat('YmdHi', $ts);
$fmTimestamp = $date->format('m/d/Y H:i');
Keep it simple stupid.
$input = $_GET['timestamp']; // 201308031525
$year = (int)substr($input,0,4);
$month = (int)substr($input,4,2);
$date = (int)substr($input,6,2);
$hour = (int)substr($input,8,2);
$minute = (int)substr($input,10);
$date_obj = new DateTime($year . '-' . $month . '-' . $date .' ' . $hour . ':' . $minute);
echo $date_obj->format('m/d/Y h:i:s A');
and the output is:
08/03/2013 03:25:00 PM
You are not instantiating the object you are trying to use.
Try this approach instead:
$date = new DateTime;
$date->createFromFormat('ymdHi', $ts);
$fmTimestamp = $date->format('m/d/Y h:i:s A');
This is untested, just saying ...
What date format is this?
2012-06-08dT00:00:00Z
And how can i convert a timestamp in php to this date format?
$dt = new DateTime('2012-06-08T00:00:00Z'); //with no 'd'
$timestamp = $dt->format('U');
If you must have the 'd', then:
$dt = DateTime::createFromFormat('Y-m-d??H:i:s?', '2012-06-08dT00:00:00Z');
$timestamp = $dt->format('U');
ETA: timestamp -> your format
$dt = new DateTime('#1339124400'); //the # indicates the following number is a timestamp
$isoformat= $dt->format('Y-m-d\TH:i:sZ'); //leave out the 'd' and escape the 'T'
here is something for you to start from:
$date = "2012-06-08dT01:02:03Z";
// parse the date correctly
$parsed_date = date_parse_from_format('Y-m-d H:i:s ', $date);
print_r($parsed_date);
// Make time from parsed date
$old_date = mktime($parsed_date['hour'], $parsed_date['minute'], $parsed_date['second'], $parsed_date['month'], $parsed_date['day'], $parsed_date['year']);
$now_date = time();
// a silly way to print that parsed date into the original way as before
echo date("Y-m-d", $old_date) . 'dT' . date("H:i:s", $old_date) . 'Z';
echo "\n";
// a silly way to print current date/time in that format
echo date("Y-m-d", $now_date) . 'dT' . date("H:i:s", $now_date) . 'Z';
I am having a problem with formatting a date that I receive from a MySQL database. I keep getting this error:
Notice: A non well formed numeric value encountered in C:\xampp\htdocs\index.php on line 23
Here is my code:
$news_query = $database->query("SELECT * FROM " . DB_NEWS . " ORDER BY 'date'");
while($news_data = mysql_fetch_array($news_query,MYSQL_ASSOC)){
$date = date("M d, Y - g:i:s A", $news_data['date']);
echo "
<DIV class = 'news_post'>
<DIV class = 'news_title'>" . $news_data['title'] . "</DIV>
<DIV class = 'news_info'>Posted on " . $date . " By " . $news_data["author"] . "</DIV>
<DIV class = 'entry'>" . $news_data['entry'] . "</DIV>
</DIV>";
}
I have read over the PHP manual for timestamps and the date function and I can't seem to figure out what my problem is.
Are you using datetime format in MySQL?
Try this:
<?php
date("M d, Y - g:i:s A", strtotime( $news_data['date'] ) );
?>
The date from the database in $news_data['date'] is probably a string representation of a date. The date() function takes a format string and an optional timestamp, which is an integer. This is the reason for the error you get.
Most likely, strtotime() will convert your date into the format required for date().
Instead of:
$date = date("M d, Y - g:i:s A", $news_data['date'])
You want:
$date = date("M d, Y - g:i:s A", strtotime($news_data['date']))
Try this query
$database->query("SELECT *,unix_timestamp(date) as date
FROM ".DB_NEWS." ORDER BY date")
There are two possibilities.
The first is : use UNIXTIMESTAMP sql function in your query, or strtotime like said before.
The second, which I prefer is using DateTime PHP object.
$date = new DateTime($datas['date']);
//if an error occurs because of the format use createFromFormat
$date = DateTime::createFromFormat('d-m-y',$datas['date']);//for exemple for dd-mm-YYYY format