How do I get the timestamp for my date - wordpress

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');

Related

How to decode booking dates in Wordpress DB

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 convert the date integer value of ACF plugin in WordPress?

I am using the jQuery Date Picker of WordPress ACF Plugin to make a custom Meta Box of a custom POST.
Now, when this Date Picker value is saved to the database table called xxx_postmeta I see that value is:
20190630
So then when I get that meta value using below code:
<?php echo date('F j, Y ', get_post_meta( get_the_ID(), 'article_1_pub_date', true)) ; ?>
then the date is showing wrong, Like this:
August 22, 1970
Is there any wrong or ACF issue?
I assume the date you saved is June 30, 2019. You can’t use PHP date() function on this string.
Convert to a date object and print:
$date = DateTime::createFromFormat('Ymd', '20190630');
echo $date->format('F j, Y ');

Get if the current user has an active subscription with trial period

I'm developing a website in WordPress with WooCommerce. I am using Woocommerce Subscription. I have a product with a trial period. Now I want to change the next payment date based on the trial period. How can I get the trial period date and Next payment date? So that I can change the Second payment date. I tried using the below-mentioned code but didn't find any solution.
<?php if (has_woocommerce_subscription('','','active') && WC_Subscriptions_Product::get_trial_length( $product_id ) > 10) {
$trial_end = WC_Subscription::get_date( 'trial_end');
echo $trial_end;
}
?>
<?php WC_Subscription::get_date( $date_type, $timezone ) ?>
$date_type
(string) (required) The type of date to get, can be 'start', 'trial_end', 'next_payment', 'last_payment' or 'end'. Default: None
$timezone
(string) (optional) The timezone to use for the returned date, either ‘gmt’ or ‘site’. Default ‘gmt’.
You will get complete document here.
https://docs.woocommerce.com/document/subscriptions/develop/functions/
Update :
$product = wc_get_product($product_id);
$period = WC_Subscriptions_Product::get_period($product);
var_dump($product);
Add output here in question. You will find start and end date as well as next payment date.

wp-prepare function error "Too few arguments"

In my Wordpress plugin I have code snippets that look like this:
$total = $wpdb->get_var($wpdb->prepare(
"
SELECT SUM(Amount)
FROM $table_name
WHERE Account = $user_id AND Timestamp > {$balance['Timestamp']}
",NULL
));
It was working very well for years, but after I recently updated Wordpress to 5.0 I get many errors like this:
Fatal error: Uncaught ArgumentCountError: Too few arguments to function wpdb::prepare(), 1 passed in (...)/pluginfile.php on line 753 and exactly 2 expected in /wp-includes/wp-db.php:1222
Through my research I found that I need to use %s and %d in the wp prepare function but I didn't figure out how to apply it properly to the code above.
We use the 'prepare' method to make sure we're not sending an illegal operation or any illegal characters.
Try modifying your code to the following:
$total = $wpdb->get_var($wpdb->prepare(
"
SELECT SUM(Amount)
FROM $table_name
WHERE Account = %d AND Timestamp > %d
",
$user_id, $balance['Timestamp']
));
Both User ID and a timestamp are integers (whole numbers) so we would use %d.
Possible format values: %s as string; %d as integer (whole number); and %f as float.
For anybody that might run into a similar problem: with the help of the comment above by #entreprenerds I was able to fix the code as follows:
$total = $wpdb->get_var($wpdb->prepare(
"
SELECT SUM(Amount)
FROM $table_name
WHERE Account = %d AND Timestamp > %d
",$user_id, $balance['Timestamp']
));
Thanks!

Outputting wordpress custom date field in different format

Okay, so I have a custom date field set up as 'm/d/Y H:i:s' and I'd like to echo that as 'M j'. How do I do this?
if it stored in the format m/d/Y H:i:s, then you can do this wherever the date is echoed in your template:
echo date('M j',strtotime($custom_date));

Resources