Local uses of photovoltaic (PV) energy within neighborhood PV prosumers become more economical than the individual operation of prosumers. In the present work, the hourly optimized total cost of energy sharing of peer-to-peer (P2P) PV prosumers for a microgrid is proposed. Initially, a dynamic internal pricing model is prepared for the energy sharing operation. Furthermore, considering the adjustable load of prosumers, an equiponderant cost model is formulated concerning economic costs and user interest. Finally, the formulated cost model is transformed into an optimization problem and is solved using the krill herd algorithm to get the ultimate optimized hourly total cost of energy sharing. This optimized cost provides the maximum economic profit to all the participating PV prosumers in the microgrid.