# Woo: Calculate hours between placed and completed

If you run an online store using WooCommerce, you may need to calculate the time difference between the order placed timestamp and the order completed timestamp for various reasons, such as tracking shipping times or analyzing customer behavior. In this blog post, we’ll show you how to use PHP code to calculate this time difference.

First, we need to get the order object for the order we want to calculate the time difference for using the `wc_get_order()` function. We can then use the `get_date_created()` and `get_date_completed()` methods to retrieve the timestamps for when the order was placed and completed, respectively.

Next, we can calculate the time difference between the two timestamps in hours using the formula `(completed_timestamp - placed_timestamp) / (60 * 60)`. We can then round the result to 2 decimal places using the `round()` function.

Here’s the PHP code to calculate the time difference between the order placed timestamp and the order completed timestamp:

``````\$order_id = 123; // Replace with the ID of the order you want to calculate the time difference for
\$order = wc_get_order(\$order_id);
\$placed_timestamp = strtotime(\$order->get_date_created());
\$completed_timestamp = strtotime(\$order->get_date_completed());
\$time_diff_hours = round((\$completed_timestamp - \$placed_timestamp) / (60 * 60), 2);

echo "Time difference between order placed and completed: {\$time_diff_hours} hours";
``````

In this code, we first get the order object for the specified order ID using the `wc_get_order()` function. We then use the `get_date_created()` and `get_date_completed()` methods to retrieve the timestamps for when the order was placed and completed, respectively.

We calculate the time difference between the two timestamps in hours using the formula `(completed_timestamp - placed_timestamp) / (60 * 60)` and round the result to 2 decimal places using the `round()` function.

Finally, we output the time difference in hours using the `echo` statement. You can modify this code as needed to integrate it into your WooCommerce site.

Share the Post: