I am trying to work with a json array, specifically for date/time.
In my json array, the url provides datetime in the following format 1662400800
I have tried this code to convert to a readable date:
$date1 = strtotime ( $data->list[1]->dt);
echo date ( 'd/M/Y h:i:s' , $date1 );
The output I am getting is 01/Jan/1970 03:00:00
What am I doing wrong?
Related
I ran this query in Wordpress DB and found out that Booking dates are stored in some coded way.
SELECT post_id, meta_id, post_title, meta_key, meta_value
FROM a40443635734677.wp_7n2ncdd4yn_postmeta, a40443635734677.wp_7n2ncdd4yn_posts
where post_id = ID and post_type = 'estate_property'
and meta_key in ('property_price', 'cancellation', 'booking_dates',
'prop_featured', 'min_days_booking') and meta_key = 'booking_dates' and post_id = 248
order by post_id;
Output: a:2:{i:1600646400;i:374;i:1600732800;i:374;}
From UI, it shows that booking dates are sept-21-2020 to sept-23-2020.
how to decode the data from DB (a:2:{i:1600646400;i:374;i:1600732800;i:374;}) to these dates?
You can use unserialize() to convert it into an array.
$dbdata = 'a:2:{i:1600646400;i:374;i:1600732800;i:374;}';
$dbdata = unserialize($dbdata);
//result
array (
1600646400 => 374,
1600732800 => 374,
)
Then use PHPs DateTime to convert to human readable format e.g.
$returnDate = date('d.m.Y H:i:s', 1600646400);
//result
21.09.2020 02:00:00
The larger set of numbers you showed us look like seconds since the Epoch (January 1, 1970). You could use MySQL's FROM_UNIXTIME function convert these epoch values to a bona-fide human readable date. For example:
SELECT FROM_UNIXTIME(1600646400); -- 21.09.2020 02:00:00
That is serialized data. Use PHPs built in unserialize( your output variable here ). You'll get an array since the serialized data is an array (a:2 means array with two indexes).
The date is in UNIX time. You can use PHPs DateTime to convert to human readable format.
$output = SELECT post_id, meta_id, post_title, meta_key, meta_value
FROM a40443635734677.wp_7n2ncdd4yn_postmeta, a40443635734677.wp_7n2ncdd4yn_posts
where post_id = ID
and post_type = 'estate_property'
and meta_key in ('property_price', 'cancellation', 'booking_dates', 'prop_featured', 'min_days_booking') and meta_key = 'booking_dates'
and post_id = 248 order by post_id;
// Unserialize the data.
$unserialized = unserialize($output);
// Get the date for each array key. The date is the $key, so you pass that to the php date function.
foreach( $unserialized as $key => $date ) {
echo date('M-d-Y', $key ) . "\n";
}
This will output Sept-20-2020 and Sept-21-2020 with the data you supplied.
How to declare date and time in symfony? When I declare them, it is giving me errors like expecting date while declaring string and all. Please provide the correct format for date.
This is what I did:
$time = "09:11";
$date = "22-07-2019"
$entity = new Entity();
$entity->setDate(new \DateTime($date));
$entity->setTime($time);
Why do you split date and time to two fields, use one field for both.
Use this to convert string to datetime:
$format = 'd-m-Y H:i'; (for example)
$date = \DateTime::createFromFormat($format, '22-07-2019 09:11');
The Sqlite normal date string format is yyyy-MM-dd. How can I save it as dd/MM/yyyy? Should I keep the yyyy-MM-dd format when working with the database and use dd/MM/yyyy only with the end user interface?
Edit 1:
I want to save my date datas as 'dd/MM/yyyy' in SQLite to be the same as my locale date format. But the date comparison is return wrong result with this format. I changed the date string in DB to 'yyyy-MM-dd' and the date comparison work perfect. Is 'yyyy-MM-dd' is the only string format which SQLite can understand as date?
You can use the strftime() function which accepts as its first parameter a format string:
SELECT strftime('%d/%m/%Y', 'now');
Output:
06/10/2015
The strftime() function gives the ability to convert Y-m-d into d/m/Y or any other format you want. So you can continue to store your date data as is in the database, and you can query it out however you wish.
you can use date formatter code:
NSString *myString = #"2012-11-22 10:19:04";
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MM-dd HH:mm:ss";
NSDate *yourDate = [dateFormatter dateFromString:myString];
dateFormatter.dateFormat = #"dd-MMM-yyyy";
NSLog(#"%#",[dateFormatter stringFromDate:yourDate]);
The current code below returns my current local time in wordpress and the result looks like this. 2013-07-29 13:45:42
I need to convert this to a timestamp format. What is the answer please?
echo date_i18n( 'Y-m-d H:i:s', current_time( 'timestamp', 0 ) );
The PHP strtotime() function might work.
$timestamp = strtotime('2013-07-29 13:45:42');
EDIT
Here is your use case
$current_date = date('Y-m-d H:i:s'); ## Get current date
$timestamp = strtotime($current_date); ## Get timestamp of current date
echo $timestamp; ## Print timestamp
Come to find out I really just needed the actual date and not a timestamp to achieve my goal. So using this gives you wordpress current local time that is set on the settings page in the admin panel.
date_i18n('Y-m-d');
My jquery function returning date in json format,so i want to know how to convert this into datetime formate "mm/dd/yyyy"
DateTime date1;
DateTime.TryParseExact(formCollection["date"], "MM/dd/yyyy", new CultureInfo("en-US"), DateTimeStyles.None, out date1);
it will not throw any exception.
if there is wrong format, you can compare with DateTime.MinValue, that is it succesfully converted.
How do I format a Microsoft JSON date?
You can use this to get a date from json:
var date = eval(jsonDate.replace(/\/Date\((\d+)\)\//gi, "new Date($1)"));
and then you can use JavaScript Date Format script (1.2 KB when minified and gzipped) to display it as you want.
There is no date data type in JSON, so what you have is a date formatted as a string. Use the ParseExact method to parse the string into a DateTime value.
Then you can use the ToString method or String.Format method using the pattern MM'/'dd'/'yyyy to format the DateTime value into a string.